简体   繁体   中英

While decoding the html entities from string, special characters display incorrectly

function decode_entities($text) {
    $text= html_entity_decode($text,ENT_QUOTES,"ISO-8859-1"); #NOTE: UTF-8 does not work!
    $text= preg_replace('/&#(\d+);/me',"chr(\\1)",$text); #decimal notation
    $text= preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)",$text);  #hex notation
    return $text;
}

echo decode_entities("For tiden er neste president i det afrikanske landet Burkina Faso 11 år
");

echo html_entity_decode("For tiden er neste president i det afrikanske landet Burkina Faso 11 år
",'UTF-8');

I am using the above function to decode HTML entities from the string but while decoding special characters are displaying incorrectly like . Demo

Try use a echo to force the displayed charset...

echo "<meta charset='UTF-8'>";
echo html_entity_decode("For tiden er neste president i det afrikanske landet Burkina Faso 11 &aring;r",'UTF-8');

For me, the UTF-8 charset agrument for html_entity_decode works just fine. Tested on your phpfiddle script. If it's not, try setting a content-encoding header using header('Content-Encoding: UTF-8');

Considering the wrong parameter place in the example, the code that works for me looks like this:

header('Content-Encoding: UTF-8');
echo html_entity_decode("For tiden er neste president i det afrikanske landet Burkina Faso 11 &aring;r", ENT_QUOTES, 'UTF-8');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM