user2935239
user2935239

Reputation: 11

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

Upvotes: 1

Views: 455

Answers (2)

Buga Dániel
Buga Dániel

Reputation: 830

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 år", ENT_QUOTES, 'UTF-8');

Upvotes: 0

giordanolima
giordanolima

Reputation: 1218

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');

Upvotes: 1

Related Questions