|
|
 |
html_entity_decode (PHP 4 >= 4.3.0, PHP 5) html_entity_decode --
Convert all HTML entities to their applicable characters
Descriptionstring html_entity_decode ( string string [, int quote_style [, string charset]] )
html_entity_decode() is the opposite of
htmlentities() in that it converts all HTML entities
to their applicable characters from string.
The optional second quote_style parameter lets
you define what will be done with 'single' and "double" quotes. It takes
on one of three constants with the default being
ENT_COMPAT:
Table 1. Available quote_style constants | Constant Name | Description |
|---|
| ENT_COMPAT | Will convert double-quotes and leave single-quotes alone. | | ENT_QUOTES | Will convert both double and single quotes. | | ENT_NOQUOTES | Will leave both double and single quotes unconverted. |
The ISO-8859-1 character set is used as default for the optional third
charset. This defines the character set used in
conversion.
Following character sets are supported in PHP 4.3.0 and later.
Table 2. Supported charsets | Charset | Aliases | Description |
|---|
| ISO-8859-1 | ISO8859-1 |
Western European, Latin-1
| | ISO-8859-15 | ISO8859-15 |
Western European, Latin-9. Adds the Euro sign, French and Finnish
letters missing in Latin-1(ISO-8859-1).
| | UTF-8 | |
ASCII compatible multi-byte 8-bit Unicode.
| | cp866 | ibm866, 866 |
DOS-specific Cyrillic charset.
This charset is supported in 4.3.2.
| | cp1251 | Windows-1251, win-1251, 1251 |
Windows-specific Cyrillic charset.
This charset is supported in 4.3.2.
| | cp1252 | Windows-1252, 1252 |
Windows specific charset for Western European.
| | KOI8-R | koi8-ru, koi8r |
Russian. This charset is supported in 4.3.2.
| | BIG5 | 950 |
Traditional Chinese, mainly used in Taiwan.
| | GB2312 | 936 |
Simplified Chinese, national standard character set.
| | BIG5-HKSCS | |
Big5 with Hong Kong extensions, Traditional Chinese.
| | Shift_JIS | SJIS, 932 |
Japanese
| | EUC-JP | EUCJP |
Japanese
|
Note:
Any other character sets are not recognized and ISO-8859-1 will be used
instead.
Example 1. Decoding HTML entities |
<?php
$orig = "I'll \"walk\" the <b>dog</b> now";
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a; echo $b; function unhtmlentities($string)
{
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
return strtr($string, $trans_tbl);
}
$c = unhtmlentities($a);
echo $c; ?>
|
|
Note:
You might wonder why trim(html_entity_decode(' ')); doesn't
reduce the string to an empty string, that's because the ' '
entity is not ASCII code 32 (which is stripped by
trim()) but ASCII code 160 (0xa0) in the default ISO
8859-1 characterset.
See also htmlentities(),
htmlspecialchars(),
get_html_translation_table(),
and urldecode().
User Contributed Notes
html_entity_decode
marius (at) hot (dot) ee
08-Apr-2005 08:40
To convert html entities into unicode characters, use the following:
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
foreach($trans_tbl as $k => $v)
{
$ttr[$v] = utf8_encode($k);
}
$text = strtr($text, $ttr);
php dot net at c dash ovidiu dot tk
18-Mar-2005 02:37
Quick & dirty code that translates numeric entities to UTF-8.
<?php
function replace_num_entity($ord)
{
$ord = $ord[1];
if (preg_match('/^x([0-9a-f]+)$/i', $ord, $match))
{
$ord = hexdec($match[1]);
}
else
{
$ord = intval($ord);
}
$no_bytes = 0;
$byte = array();
if ($ord < 128)
{
return chr($ord);
}
elseif ($ord < 2048)
{
$no_bytes = 2;
}
elseif ($ord < 65536)
{
$no_bytes = 3;
}
elseif ($ord < 1114112)
{
$no_bytes = 4;
}
else
{
return;
}
switch($no_bytes)
{
case 2:
{
$prefix = array(31, 192);
break;
}
case 3:
{
$prefix = array(15, 224);
break;
}
case 4:
{
$prefix = array(7, 240);
}
}
for ($i = 0; $i < $no_bytes; $i++)
{
$byte[$no_bytes - $i - 1] = (($ord & (63 * pow(2, 6 * $i))) / pow(2, 6 * $i)) & 63 | 128;
}
$byte[0] = ($byte[0] & $prefix[0]) | $prefix[1];
$ret = '';
for ($i = 0; $i < $no_bytes; $i++)
{
$ret .= chr($byte[$i]);
}
return $ret;
}
$test = 'This is a čא test'';
echo $test . "<br />\n";
echo preg_replace_callback('/&#([0-9a-fx]+);/mi', 'replace_num_entity', $test);
?>
Silvan
28-Jan-2005 09:33
Passing NULL or FALSE as a string will generate a '500 Internal Server Error' (or break the script when inside a function).
So always test your string first before passing it to html_entity_decode().
daniel at brightbyte dot de
13-Nov-2004 08:12
This function seems to have to have two limitations (at least in PHP 4.3.8):
a) it does not work with multibyte character codings, such as UTF-8
b) it does not decode numeric entity references
a) can be solved by using iconv to convert to ISO-8859-1, then decoding the entities, than convert to UTF-8 again. But that's quite ugly and detroys all characters not present in Latin-1.
b) can be solved rather nicely using the following code:
<?php
function decode_entities($text) {
$text= html_entity_decode($text,ENT_QUOTES,"ISO-8859-1"); $text= preg_replace('/&#(\d+);/me',"chr(\\1)",$text); $text= preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)",$text); return $text;
}
?>
HTH
aidan at php dot net
14-Sep-2004 02:57
This functionality is now implemented in the PEAR package PHP_Compat.
More information about using this function without upgrading your version of PHP can be found on the below link:
http://pear.php.net/package/PHP_Compat
| |