htmlspecialchars

(PHP 4, PHP 5, PHP 7, PHP 8)

htmlspecialchars½«ÌØÊâ×Ö·ûת»»Îª HTML ʵÌå

˵Ã÷

htmlspecialchars ( string $string , int $flags = ENT_COMPAT | ENT_HTML401 , string $encoding = ini_get("default_charset") , bool $double_encode = true ) : string

ijÀà×Ö·ûÔÚ HTML ÖÐÓÐÌØÊâÓô¦£¬ÈçÐè±£³ÖÔ­Ò⣬ÐèÒªÓà HTML ʵÌåÀ´±í´ï¡£ ±¾º¯Êý»á·µ»Ø×Ö·ûתÒåºóµÄ±í´ï¡£ ÈçÐèת»»×Ó×Ö·û´®ÖÐËùÓйØÁªµÄÃû³ÆʵÌ壬ʹÓà htmlentities() ´úÌæ±¾º¯Êý¡£

Èç¹û´«Èë×Ö·ûµÄ×Ö·û±àÂëºÍ×îÖÕµÄÎĵµÊÇÒ»Öµģ¬ÔòÓú¯Êý´¦ÀíµÄÊäÈëÊʺϾø´ó¶àÊý HTML Îĵµ»·¾³¡£ È»¶ø£¬Èç¹ûÊäÈëµÄ×Ö·û±àÂëºÍ×îÖÕ°üº¬×Ö·ûµÄÎĵµÊDz»Ò»ÑùµÄ£¬ ÏëÒª±£Áô×Ö·û£¨ÒÔÊý×Ö»òÃû³ÆʵÌåµÄÐÎʽ£©£¬±¾º¯ÊýÒÔ¼° htmlentities() £¨½ö±àÂëÃû³ÆʵÌå¶ÔÓ¦µÄ×Ó×Ö·û´®£©¿ÉÄܲ»¹»ÓᣠÕâÖÖÇé¿ö¿ÉÒÔʹÓà mb_encode_numericentity() ´úÌæ¡£

Ö´ÐÐת»»
×Ö·û Ìæ»»ºó
& (& ·ûºÅ) &
" (Ë«ÒýºÅ) "£¬³ý·ÇÉèÖÃÁË ENT_NOQUOTES
' (µ¥ÒýºÅ) ÉèÖÃÁË ENT_QUOTES ºó£¬ ' (Èç¹ûÊÇ ENT_HTML401) £¬»òÕß ' (Èç¹ûÊÇ ENT_XML1¡¢ ENT_XHTML »ò ENT_HTML5)¡£
< (СÓÚ) &lt;
> (´óÓÚ) &gt;

²ÎÊý

string

´ýת»»µÄ string¡£

flags

λÑÚÂ룬ÓÉÒÔÏÂij¸ö»ò¶à¸ö±ê¼Ç×é³É£¬ÉèÖÃתÒå´¦Àíϸ½Ú¡¢ÎÞЧµ¥ÔªÐòÁС¢ÎĵµÀàÐÍ¡£ ĬÈÏÊÇ ENT_COMPAT | ENT_HTML401¡£

ÓÐЧµÄ flags ³£Á¿
³£Á¿Ãû³Æ ÃèÊö
ENT_COMPAT »áת»»Ë«ÒýºÅ£¬²»×ª»»µ¥ÒýºÅ¡£
ENT_QUOTES ¼Èת»»Ë«ÒýºÅҲת»»µ¥ÒýºÅ¡£
ENT_NOQUOTES µ¥/Ë«ÒýºÅ¶¼²»×ª»»
ENT_IGNORE ¾²Ä¬¶ªÆúÎÞЧµÄ´úÂëµ¥ÔªÐòÁУ¬¶ø²»ÊÇ·µ»Ø¿Õ×Ö·û´®¡£ ²»½¨ÒéʹÓô˱ê¼Ç£¬ ÒòΪËü» ¿ÉÄÜÓа²È«Ó°Ïì¡£
ENT_SUBSTITUTE Ìæ»»ÎÞЧµÄ´úÂëµ¥ÔªÐòÁÐΪ Unicode ´úÌæ·û£¨Replacement Character£©£¬ U+FFFD (UTF-8) »òÕß &#xFFFD; (ÆäËû)£¬¶ø²»ÊÇ·µ»Ø¿Õ×Ö·û´®¡£
ENT_DISALLOWED ΪÎĵµµÄÎÞЧ´úÂëµãÌ滻Ϊ Unicode ´úÌæ·û£¨Replacement Character£©£º U+FFFD (UTF-8)£¬»ò &#xFFFD;£¨ÆäËû£©£¬¶ø²»ÊÇ°ÑËüÃÇÁôÔÚÔ­´¦¡£ ±ÈÈçÒÔÏÂÇé¿öϾͺÜÓÐÓãºÒª±£Ö¤ XML ÎĵµÇ¶Èë¶îÍâÄÚÈÝʱ¸ñʽºÏ·¨¡£
ENT_HTML401 ÒÔ HTML 4.01 ´¦Àí´úÂë¡£
ENT_XML1 ÒÔ XML 1 ´¦Àí´úÂë¡£
ENT_XHTML ÒÔ XHTML ´¦Àí´úÂë¡£
ENT_HTML5 ÒÔ HTML 5 ´¦Àí´úÂë¡£

encoding

An optional argument defining the encoding used when converting characters.

If omitted, encoding defaults to the value of the default_charset configuration option.

Although this argument is technically optional, you are highly encouraged to specify the correct value for your code if the default_charset configuration option may be set incorrectly for the given input.

±¾º¯ÊýʹÓÃЧ¹ûÉÏ£¬Èç¹û string ¶ÔÒÔÏÂ×Ö·û±àÂëÊÇÓÐЧµÄ£¬ ISO-8859-1¡¢ ISO-8859-15¡¢ UTF-8¡¢ cp866¡¢ cp1251¡¢ cp1252¡¢ KOI8-R ½«¾ßÓÐÏàͬµÄЧ¹û¡£ Ò²¾ÍÊÇ˵£¬ÔÚÕâЩ±àÂëÀ ÊÜ htmlspecialchars() Ó°ÏìµÄ×Ö·û»áÕ¼¾ÝÏàͬµÄλÖá£

Ö§³ÖÒÔÏÂ×Ö·û¼¯£º

Ö§³ÖµÄ×Ö·û¼¯Áбí
×Ö·û¼¯ ±ðÃû ÃèÊö
ISO-8859-1 ISO8859-1 Î÷Å·£¬Latin-1
ISO-8859-5 ISO8859-5 Little used cyrillic charset (Latin/Cyrillic).
ISO-8859-15 ISO8859-15 Î÷Å·£¬Latin-9¡£Ôö¼ÓÅ·Ôª·ûºÅ£¬·¨ÓïºÍ·ÒÀ¼Óï×ÖĸÔÚ Latin-1(ISO-8859-1) ÖÐȱʧ¡£
UTF-8   ASCII ¼æÈݵĶà×Ö½Ú 8 λ Unicode¡£
cp866 ibm866, 866 DOS ÌØÓеÄÎ÷Àï¶û±àÂë¡£±¾×Ö·û¼¯ÔÚ 4.3.2 °æ±¾Öеõ½Ö§³Ö¡£
cp1251 Windows-1251, win-1251, 1251 Windows ÌØÓеÄÎ÷Àï¶û±àÂë¡£±¾×Ö·û¼¯ÔÚ 4.3.2 °æ±¾Öеõ½Ö§³Ö¡£
cp1252 Windows-1252, 1252 Windows ÌØÓеÄÎ÷Å·±àÂë¡£
KOI8-R koi8-ru, koi8r ¶íÓï¡£±¾×Ö·û¼¯ÔÚ 4.3.2 °æ±¾Öеõ½Ö§³Ö¡£
BIG5 950 ·±ÌåÖÐÎÄ£¬Ö÷ÒªÓÃÓÚÖйų́ÍåÊ¡¡£
GB2312 936 ¼òÌåÖÐÎÄ£¬Öйú¹ú¼Ò±ê×¼×Ö·û¼¯¡£
BIG5-HKSCS   ·±ÌåÖÐÎÄ£¬¸½´øÏã¸ÛÀ©Õ¹µÄ Big5 ×Ö·û¼¯¡£
Shift_JIS SJIS, 932 ÈÕÓï
EUC-JP EUCJP ÈÕÓï
MacRoman   Mac OS ʹÓõÄ×Ö·û´®¡£
''   An empty string activates detection from script encoding (Zend multibyte), default_charset and current locale (see nl_langinfo() and setlocale()), in this order. Not recommended.

Note: ÆäËû×Ö·û¼¯Ã»ÓÐÈÏ¿É¡£½«»áʹÓÃĬÈϱàÂë²¢Å׳öÒì³£¡£

double_encode

¹Ø±Õ double_encode ʱ£¬PHP ²»»áת»»ÏÖÓÐµÄ HTML ʵÌ壬 ĬÈÏÊÇÈ«²¿×ª»»¡£

·µ»ØÖµ

ת»»ºóµÄ string¡£

Èç¹ûÖ¸¶¨µÄ±àÂë encoding À string °üº¬ÁËÎÞЧµÄ´úÂëµ¥ÔªÐòÁУ¬ ûÓÐÉèÖà ENT_IGNORE »òÕß ENT_SUBSTITUTE ±ê¼ÇµÄÇé¿öÏ£¬»á·µ»Ø¿Õ×Ö·û´®¡£

¸üÐÂÈÕÖ¾

°æ±¾ ˵Ã÷
5.6.0 The default value for the encoding parameter was changed to be the value of the default_charset configuration option.
5.4.0 encoding ²ÎÊýµÄĬÈÏÖµ¸Ä³É UTF-8¡£
5.4.0 Ôö¼Ó³£Á¿ ENT_SUBSTITUTE¡¢ ENT_DISALLOWED¡¢ ENT_HTML401¡¢ ENT_XML1¡¢ ENT_XHTML¡¢ ENT_HTML5¡£
5.3.0 Ôö¼Ó³£Á¿ ENT_IGNORE¡£
5.2.3 Ôö¼Ó²ÎÊý double_encode¡£

·¶Àý

Example #1 htmlspecialchars() Àý×Ó

<?php
$new 
htmlspecialchars("<a href='test'>Test</a>"ENT_QUOTES);
echo 
$new// &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
?>

×¢ÊÍ

Note:

×¢Ò⣬±¾º¯Êý²»»áת»»ÒÔÉÏÁбíÒÔÍâµÄʵÌå¡£ ÍêÕûת»»Çë²Î¼û htmlentities()¡£

Note:

Èç¹û flags µÄÉèÖÃÄ£ºýÒ×»ìÏý£¬½«×ñÑ­ÒÔϹæÔò£º

  • µ± ENT_COMPAT¡¢ENT_QUOTES¡¢ENT_NOQUOTES ¶¼Ã»ÉèÖ㬠ĬÈϾÍÊÇ ENT_NOQUOTES¡£
  • Èç¹ûÉèÖò»Ö¹Ò»¸ö ENT_COMPAT¡¢ ENT_QUOTES¡¢ ENT_NOQUOTES £¬ÓÅÏȼ¶×î¸ßµÄÊÇ ENT_QUOTES£¬ Æä´ÎÊÇ ENT_COMPAT¡£
  • µ± ENT_HTML401¡¢ ENT_HTML5¡¢ ENT_XHTML¡¢ ENT_XML1 ¶¼Ã»ÉèÖã¬Ä¬ÈÏÊÇ ENT_HTML401¡£
  • Èç¹ûÉèÖò»Ö¹Ò»¸ö ENT_HTML401¡¢ ENT_HTML5¡¢ ENT_XHTML¡¢ ENT_XML1£¬ ÓÅÏȼ¶×î¸ßµÄÊÇ ENT_HTML5 Æä´ÎÊÇ ENT_XHTML¡¢ENT_XML1 ºÍ ENT_HTML401¡£
  • Èç¹ûÉèÖò»Ö¹Ò»¸ö ENT_DISALLOWED¡¢ ENT_IGNORE¡¢ ENT_SUBSTITUTE£¬ÓÅÏȼ¶×î¸ßµÄÊÇ ENT_IGNORE£¬ Æä´ÎÊÇ ENT_SUBSTITUTE¡£

²Î¼û

User Contributed Notes

ASchmidt at Anamera dot net 02-Apr-2021 08:48
One MUST specify ENT_HTML5 in addition to double_encode=false to avoid double-encoding.

The reason is that contrary to the documentation, double_encode=false will NOT unconditionally and globally prevent double-encoding of ALL existing entities. Crucially, it will only skip double-encoding for THOSE character entities that are explicitly valid for the document type chosen!

Since ENT_HTML5 references the most expansive list of character entities, it is the only setting that will be most lenient with existing character entities.

<?php
declare(strict_types=1);
$text = 'ampersand(&amp;), double quote(&quot;), single quote(&apos;), less than(&lt;), greater than(&gt;), numeric entities(&#x26;&#x22;&#x27;&#x3C;&#x3E;), HTML 5 entities(&plus;&comma;&excl;&dollar;&lpar;&ncedil;&euro;)';
$result3 = htmlspecialchars( $text, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8', /*double_encode*/false );
$result4 = htmlspecialchars( $text, ENT_NOQUOTES | ENT_XML1 | ENT_SUBSTITUTE, 'UTF-8', /*double_encode*/false );
$result5 = htmlspecialchars( $text, ENT_NOQUOTES | ENT_XHTML | ENT_SUBSTITUTE, 'UTF-8', /*double_encode*/false );
$result6 = htmlspecialchars( $text, ENT_NOQUOTES | ENT_HTML5 | ENT_SUBSTITUTE, 'UTF-8', /*double_encode*/false );

echo
"<br />\r\nHTML 4.01:<br />\r\n", $result3,
   
"<br />\r\nXML 1:<br />\r\n", $result4,
   
"<br />\r\nXHTML:<br />\r\n", $result5,
   
"<br />\r\nHTML 5:<br />\r\n", $result6, "<br />\r\n";
?>

will produce:

HTML 4.01 (will NOT recognize single quote, but Euro):
ampersand(&), double quote("), single quote(&apos;), less than(<), greater than(>), numeric entities(&"'<>), HTML 5 entities(&plus;&comma;&excl;&dollar;&lpar;&ncedil;€)

XML 1 (WILL recognize single quote, but NOT Euro):
ampersand(&), double quote("), single quote('), less than(<), greater than(>), numeric entities(&"'<>), HTML 5 entities(&plus;&comma;&excl;&dollar;&lpar;&ncedil;&euro;)

XHTML (recognizes single quote and Euro):
ampersand(&), double quote("), single quote('), less than(<), greater than(>), numeric entities(&"'<>), HTML 5 entities(&plus;&comma;&excl;&dollar;&lpar;&ncedil;€)

HTML 5 (recognizes "all" valid character entities):
ampersand(&), double quote("), single quote('), less than(<), greater than(>), numeric entities(&"'<>), HTML 5 entities(+,!$(?€)
qshing1437 at hotmail dot com 22-Jul-2019 05:11
If you use htmlspecialchars() to escape any HTML attribute, make sure use double quote instead of single quote for the attribute.

For Example,

> Wrap with Single Quote
<?php
echo "<p title='"  . htmlspecialchars("Hello\"s\'world") . "'">

// title will end up Hello"s\ and rest of the text after single quote will be cut off.
?>

> Wrap with Double quote :
<?php
echo '<p title="'  . htmlspecialchars("Hello\"s\'world") . '"'>

// title will show up correctly as Hello"s'world
?>
PoV 17-May-2015 11:59
Be aware of the encoding of your source files!!!

Some of the suggestions here make reference to workarounds where you hard-code an encoding.

<?php
 
echo htmlspecialchars('<b>W?rmann</b>');  // Why isn't this working?
?>

As it turns out, it may actually be your text editor that is to blame.

As of PHP 5.4, htmlspecialchars now defaults to the UTF-8 encoding. That said, many text editors default to non-UTF encodings like ISO-8859-1 (i.e. Latin-1) or WIN-1252. If you change the encoding of the file to UTF-8, the code above will now work (i.e. the ? is encoded differently in UTF-8 and ISO-8859-1, and you need the UTF-8 version).

Make sure you are editing in UTF-8 Unicode mode! Check your UI or manual for how to convert files to Unicode. It's also a good idea to figure out where to look in your UI to see what the current file encoding is.
Felix D. 09-Jan-2014 11:00
Another thing important to mention is that
htmlspecialchars(NULL)
returnes an empty string and not NULL!
support at playnext dot ru 03-Oct-2013 08:31
For those having problems after the change of default value of $encoding argument to UTF-8 since PHP 5.4.

If your old non-UTF8 projects ruined - pls consider:
1. http://php.net/manual/en/function.override-function.php
2. http://php.net/manual/ru/function.runkit-function-redefine.php

The idea - you override the built-in htmlspecialchars() function with your customized variant which is able to respect non UTF-8 default encoding. This small piece of code can be then easily inserted somewhere at the start of yout project. No need to rewrite all htmlspecialchars() entries globally.

I've spent several hours with both approaches. Variant 1 looks good especaially in combination with http://www.php.net/manual/en/function.rename-function.php as it allows to call original htmlspecialchars() with just altered default args. The code could be as follows:

<?php
rename_function
('htmlspecialchars', 'renamed_htmlspecialchars');
function
overriden_htmlspecialchars($string, $flags=NULL, $encoding='cp1251', $double_encode=true) {
   
$flags = $flags ? $flags : (ENT_COMPAT|ENT_HTML401);
    return
renamed_htmlspecialchars($string, $flags, $encoding, $double_encode);
}
override_function('htmlspecialchars', '$string, $flags, $encoding, $double_encode', 'return overriden_htmlspecialchars($string, $flags, $encoding, $double_encode);');
?>

Unfortunatelly this didn't work for me properly - my site managed to call overriden function but not every time I reloaded the pages. Moreover other PHP sites crashed under my Apache server as they suddenly started blaming htmlspecialchars() was not defined. I suppose I had to spend more time to make it work thread/request/site/whatever-safe.

So I switched to runkit (variant 2). It worked for me, although even after trying runkit_function_rename()+runkit_function_add() I didn't managed to recall original htmlspecialchars() function. So as a quick solution I decided to call htmlentities() instead:

<?php
function overriden_htmlspecialchars($string, $flags=NULL, $encoding='UTF-8', $double_encode=true) {
   
$flags = $flags ? $flags : (ENT_COMPAT|ENT_HTML401);
   
$encoding = $encoding ? $encoding : 'cp1251';
    return
htmlentities($string, $flags, $encoding, $double_encode);
}
runkit_function_redefine('htmlspecialchars', '$string, $flags, $encoding, $double_encode', 'return overriden_htmlspecialchars($string, $flags, $encoding, $double_encode);');
?>

You may be able to implement your more powerfull overriden function.
Good luck!
Dave 20-Jun-2013 11:59
As of PHP 5.4 they changed default encoding from "ISO-8859-1" to "UTF-8". So if you get null from htmlspecialchars or htmlentities

where you have only set
<?php
echo htmlspecialchars($string);
echo
htmlentities($string);
?>

you can fix it by
<?php
echo htmlspecialchars($string, ENT_COMPAT,'ISO-8859-1', true);
echo
htmlentities($string, ENT_COMPAT,'ISO-8859-1', true);
?>

On linux you can find the scripts you need to fix by

grep -Rl "htmlspecialchars\\|htmlentities" /path/to/php/scripts/
minder at ufive dot unibe dot ch 19-May-2013 01:06
Problem

In many PHP legacy products the function htmlspecialchars($string) is used to convert characters like < and > and quotes a.s.o to HTML-entities. That avoids the interpretation of HTML Tags and asymmetric quote situations.

Since PHP 5.4 for $string in htmlspecialchars($string) utf8 characters are expected if no charset is defined explicitly as third parameter in the function. Legacy products are mostly in Latin1 (alias iso-8859-1) what makes the functions htmlspecialchars(), htmlentites() and html_entity_decode() to return empty strings if a special character, e. g. a German Umlaut, is present in $string:

PHP<5.4

echo htmlspecialchars('<b>Woermann</b>') //Output: &lt;b&gt;Woermann&lt;b&gt;
echo htmlspecialchars('W?rmann') //Output: &lt;b&gt;W?rmann&lt;b&gt;

PHP=5.4

echo htmlspecialchars('<b>Woermann</b>') //Output: &lt;b&gt;Woermann&lt;b&gt;
echo htmlspecialchars('<b>W?rmann</b>') //Output: empty

Three alternative solutions

a) Not runnig legacy products on PHP 5.4
b) Change all find spots in your code from
htmlspecialchars($string) and *** to
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'ISO-8859-1')
c) Replace all htmlspecialchars() and *** with a new self-made function

*** The same is true for htmlentities() and html_entity_decode();

Solution c

1 Make Search and Replace in the concerned legacy project:
Search for:        htmlspecialchars
Replace with:   htmlXspecialchars
Search for:        htmlentities
Replace with:   htmlXentities
Search for:        html_entity_decode
Replace with:   htmlX_entity_decode
2a Copy and paste the following three functions into an existing already everywhere included PHP-file in your legacy project. (of course that PHP-file must be included only once per request, otherwise you will get a Redeclare Function Fatal Error).

function htmlXspecialchars($string, $ent=ENT_COMPAT, $charset='ISO-8859-1') {
return htmlspecialchars($string, $ent, $charset);
}

function htmlXentities($string, $ent=ENT_COMPAT, $charset='ISO-8859-1') {
return htmlentities($string, $ent, $charset);
}

function htmlX_entity_decode($string, $ent=ENT_COMPAT, $charset='ISO-8859-1') {
return html_entity_decode($string, $ent, $charset);
}

or 2b crate a new PHP-file containing the three functions mentioned above, let's say, z. B. htmlXfunctions.inc.php and include it on the first line of every PHP-file in your legacy product like this: require_once('htmlXfunctions.inc.php').
Mike Robinson 14-Mar-2013 12:46
Unfortunately, as far as I can tell, the PHP devs did not provide ANY way to set the default encoding used by htmlspecialchars() or htmlentities(), even though they changed the default encoding in PHP 5.4 (*golf clap for PHP devs*). To save someone the time of trying it, this does not work:

<?php
ini_set
('default_charset', $charset); // doesn't work.
?>

Unfortunately, the only way to not have to explicitly provide the second and third parameter every single time this function is called (which gets extremely tedious) is to write your own function as a wrapper:

<?php
define
('CHARSET', 'ISO-8859-1');
define('REPLACE_FLAGS', ENT_COMPAT | ENT_XHTML);

function
html($string) {
    return
htmlspecialchars($string, REPLACE_FLAGS, CHARSET);
}

echo
html("?"); // works
?>

You can do the same for htmlentities()
ivan at lutrov dot com 15-Jun-2011 01:30
Be careful, the "charset" argument IS case sensitive. This is counter-intuitive and serves no practical purpose because the HTML spec actually has the opposite.
Thomasvdbulk at gmail dot com 28-Dec-2010 10:05
i searched for a while for a script, that could see the difference between an html tag and just < and > placed in the text,
the reason is that i recieve text from a database,
wich is inserted by an html form, and contains text and html tags,
the text can contain < and >, so does the tags,
with htmlspecialchars you can validate your text to XHTML,
but you'll also change the tags, like <b> to &lt;b&gt;,
so i needed a script that could see the difference between those two...
but i couldn't find one so i made my own one,
i havent fully tested it, but the parts i tested worked perfect!
just for people that were searching for something like this,
it may looks big, could be done easier, but it works for me, so im happy.

<?php
function fixtags($text){
$text = htmlspecialchars($text);
$text = preg_replace("/=/", "=\"\"", $text);
$text = preg_replace("/&quot;/", "&quot;\"", $text);
$tags = "/&lt;(\/|)(\w*)(\ |)(\w*)([\\\=]*)(?|(\")\"&quot;\"|)(?|(.*)?&quot;(\")|)([\ ]?)(\/|)&gt;/i";
$replacement = "<$1$2$3$4$5$6$7$8$9$10>";
$text = preg_replace($tags, $replacement, $text);
$text = preg_replace("/=\"\"/", "=", $text);
return
$text;
}
?>

an example:

<?php
$string
= "
this is smaller < than this<br />
this is greater > than this<br />
this is the same = as this<br />
<a href=\"http://www.example.com/example.php?test=test\">This is a link</a><br />
<b>Bold</b> <i>italic</i> etc..."
;
echo
fixtags($string);
?>

will echo:
this is smaller &lt; than this<br />
this is greater &gt; than this<br />
this is the same = as this<br />
<a href="http://www.example.com/example.php?test=test">This is a link</a><br />
<b>Bold</b> <i>italic</i> etc...

I hope its helpfull!!
nachitox2000 [at] hotmail [dot] com 01-Jul-2010 05:57
I had problems with spanish special characters. So i think in using htmlspecialchars but my strings also contain HTML.
So I used this :) Hope it help

<?php
function htmlspanishchars($str)
{
    return
str_replace(array("&lt;", "&gt;"), array("<", ">"), htmlspecialchars($str, ENT_NOQUOTES, "UTF-8"));
}
?>
Anonymous 18-Sep-2009 10:16
This may seem obvious, but it caused me some frustration. If you try and use htmlspecialchars with the $charset argument set and the string you run it on is not actually the same charset you specify, you get any empty string returned without any notice/warning/error.

<?php

$ok_utf8
= "A valid UTF-8 string";
$bad_utf8 = "An invalid UTF-8 string";

var_dump(htmlspecialchars($bad_utf8, ENT_NOQUOTES, 'UTF-8'));  // string(0) ""

var_dump(htmlspecialchars($ok_utf8, ENT_NOQUOTES, 'UTF-8'));  // string(20) "A valid UTF-8 string"

?>

So make sure your charsets are consistent

<?php

$bad_utf8
= "An invalid UTF-8 string";

// make sure it's really UTF-8
$bad_utf8 = mb_convert_encoding($bad_utf8, 'UTF-8', mb_detect_encoding($bad_utf8));

var_dump(htmlspecialchars($bad_utf8, ENT_NOQUOTES, 'UTF-8'));  // string(23) "An invalid UTF-8 string"

?>

I had this problem because a Mac user was submitting posts copy/pasted from a program and it contained weird chars in it.
Anonymous 16-Sep-2009 09:43
Just a few notes on how one can use htmlspecialchars() and htmlentities() to filter user input on forms for later display and/or database storage...

1. Use htmlspecialchars() to filter text input values for html input tags.  i.e.,

echo '<input name=userdata type=text value="'.htmlspecialchars($data).'" />';

 
2. Use htmlentities() to filter the same data values for most other kinds of html tags, i.e.,

echo '<p>'.htmlentities($data).'</p>';

3. Use your database escape string function to filter the data for database updates & insertions, for instance, using postgresql,

pg_query($connection,"UPDATE datatable SET datavalue='".pg_escape_string($data)."'");
 

This strategy seems to work well and consistently, without restricting anything the user might like to type and display, while still providing a good deal of protection against a wide variety of html and database escape sequence injections, which might otherwise be introduced through deliberate and/or accidental input of such character sequences by users submitting their input data via html forms.
Kenneth Kin Lum 08-Oct-2008 06:45
if your goal is just to protect your page from Cross Site Scripting (XSS) attack, or just to show HTML tags on a web page (showing <body> on the page, for example), then using htmlspecialchars() is good enough and better than using htmlentities().  A minor point is htmlspecialchars() is faster than htmlentities().  A more important point is, when we use  htmlspecialchars($s) in our code, it is automatically compatible with UTF-8 string.  Otherwise, if we use htmlentities($s), and there happens to be foreign characters in the string $s in UTF-8 encoding, then htmlentities() is going to mess it up, as it modifies the byte 0x80 to 0xFF in the string to entities like &eacute;.  (unless you specifically provide a second argument and a third argument to htmlentities(), with the third argument being "UTF-8").

The reason htmlspecialchars($s) already works with UTF-8 string is that, it changes bytes that are in the range 0x00 to 0x7F to &lt; etc, while leaving bytes in the range 0x80 to 0xFF unchanged.  We may wonder whether htmlspecialchars() may accidentally change any byte in a 2 to 4 byte UTF-8 character to &lt; etc.  The answer is, it won't.  When a UTF-8 character is 2 to 4 bytes long, all the bytes in this character is in the 0x80 to 0xFF range. None can be in the 0x00 to 0x7F range.  When a UTF-8 character is 1 byte long, it is just the same as ASCII, which is 7 bit, from 0x00 to 0x7F.  As a result, when a UTF-8 character is 1 byte long, htmlspecialchars($s) will do its job, and when the UTF-8 character is 2 to 4 bytes long, htmlspecialchars($s) will just pass those bytes unchanged.  So htmlspecialchars($s) will do the same job no matter whether $s is in ASCII, ISO-8859-1 (Latin-1), or UTF-8.
php dot net at orakio dot net 10-Apr-2008 11:26
I was recently exploring some code when I saw this being used to make data safe for "SQL".

This function should not be used to make data SQL safe (although to prevent phishing it is perfectly good).

Here is an example of how NOT to use this function:

<?php
$username
= htmlspecialchars(trim("$_POST[username]"));

$uniqueuser = $realm_db->query("SELECT `login` FROM `accounts` WHERE `login` = '$username'");
?>

(Only other check on $_POST['username'] is to make sure it isn't empty which it is after trim on a white space only name)

The problem here is that it is left to default which allows single quote marks which are used in the sql query. Turning on magic quotes might fix it but you should not rely on magic quotes, in fact you should never use it and fix the code instead. There are also problems with \ not being escaped. Even if magic quotes were used there would be the problem of allowing usernames longer than the limit and having some really weird usernames given they are to be used outside of html, this just provide a front end for registering to another system using mysql. Of course using it on the output wouldn;t cause that problem.

Another way to make something of a fix would be to use ENT_QUOTE or do:

<?php
$uniqueuser
= $realm_db->query('SELECT `login` FROM `accounts` WHERE `login` = "'.$username.'";');
?>

Eitherway none of these solutions are good practice and are not entirely unflawed. This function should simply never be used in such a fashion.

I hope this will prevent newbies using this function incorrectly (as they apparently do).
solar-energy 16-Jun-2007 03:21
also see function "urlencode()", useful for passing text with ampersand and other special chars through url

(i.e. the text is encoded as if sent from form using GET method)

e.g.

<?php
echo "<a href='foo.php?text=".urlencode("foo?&bar!")."'>link</a>";
?>

produces

<a href='foo.php?text=foo%3F%26bar%21'>link</a>

and if the link is followed, the $_GET["text"] in foo.php will contain "foo?&bar!"
11-Mar-2005 04:22
function htmlspecialchars_array($arr = array()) {
   $rs =  array();
   while(list($key,$val) = each($arr)) {
       if(is_array($val)) {
           $rs[$key] = htmlspecialchars_array($val);
       }
       else {
           $rs[$key] = htmlspecialchars($val, ENT_QUOTES);
       }   
   }
   return $rs;
}
_____ at luukku dot com 14-Sep-2002 02:21
People, don't use ereg_replace for the most simple string replacing operations (replacing constant string with another).
Use str_replace.
ryan at ryano dot net 29-Jun-2001 03:06
Actually, if you're using >= 4.0.5, this should theoretically be quicker (less overhead anyway):

$text = str_replace(array("&gt;", "&lt;", "&quot;", "&amp;"), array(">", "<", "\"", "&"), $text);