6

I have this code to decode numeric html entities to the UTF8 equivalent character.

I'm trying to convert this character:

’

which should output:

’

However, it just disappears (no output). (i've checked the source code of the page, the page has the correct utf8 character set headers/meta tags).

Does anyone know what is wrong with the code?

function entity_decode($string, $quote_style = ENT_COMPAT, $charset = "UTF-8") {    
     $string = html_entity_decode($string, $quote_style, $charset);

     $string = preg_replace_callback('~&#x([0-9a-fA-F]+);~i', "chr_utf8_callback", $string);
     $string = preg_replace('~&#([0-9]+);~e', 'chr_utf8("\\1")', $string);

    //this is another method, which also doesn't work.. 
     //$string = preg_replace_callback("/(\&#[0-9]+;)/", "entity_decode_callback", $string);

     return $string; 
}




function chr_utf8_callback($matches) { 
     return chr_utf8(hexdec($matches[1])); 
}

function chr_utf8($num) {   
     if ($num < 128) return chr($num);
     if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
     if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
     if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
     return '';
}

function entity_decode_callback($m) { 
     return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); 
} 

 echo '=' . entity_decode('&#146;');

1 Answers1

7

The PHP function mb_convert_encoding() already does what you're looking for:

$string = '&#146;';

echo mb_convert_encoding($string, 'UTF-8', 'HTML-ENTITIES');

it works a little bit better than html_entity_decode() that also does what you're looking for, but it may not have support for all numeric entities (see below):

$string = '&#146;';

echo html_entity_decode($string, ENT_COMPAT, 'UTF-8');

It will return the character:

’   binary hex: c292

Which is PRIVATE USE TWO (U+0092). As it's private use, your PHP configuration/version/compile might not return it at all.

Also there are some more quirks:

But in HTML (other than XHTML, which uses XML rules), it's a long-standing browser quirk that character references in the range &#128; to &#159; are misinterpreted to mean the characters associated with bytes 128 to 159 in the Windows Western code page (cp1252) instead of the Unicode characters with those code points. The HTML5 standard finally documents this behaviour.

See: &#146; is getting converted as “\u0092” by nokogiri in ruby on rails

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    Tried with just html_entity_decode but that doesn't work, it returns empty as well. Not sure what you're talking about a space, there is no space in my code? I've also tried removing the html_entity_decode or placing it as the last one to execute, but no help. Thx. –  Mar 06 '12 at 16:34
  • @Wesley: Depending on your PHP version, `html_entity_decode` does return something. However I've extended the answer which might shed some more light. – hakre Mar 06 '12 at 16:38
  • Also this [answer to "Convert (doublebyte) string to Hex"](http://stackoverflow.com/a/7015137/367456) might be helpful. – hakre Mar 06 '12 at 16:39
  • I'm on version 5.3.5 - but thanks, that link does explain more! This is a government document, figures as much! –  Mar 06 '12 at 16:40
  • 1
    I'm on version 5.3.10 and it does return that value. However I assume this is also platform dependent. So it might not work on your dev machine while it works on the remote machine and vice-versa. – hakre Mar 06 '12 at 16:45
  • For at least php >5.6 the above html_entity_decode example just returns ’ The reason seems to be the range 128 to 159 which are control characters and may not be visualized as plain character in UTF-8. See https://caves.org.uk/charset_test.html Section Another Problem with PHP's htmlentities – Thomas Lauria Dec 18 '19 at 11:23
  • @ThomasLauria: The best reason I can find is that these (incl. `•`) are **not** numeric HTML entities. That is what the quote relates to for *"The HTML5 standard finally documents this behaviour."*. Find it in https://html.spec.whatwg.org/multipage/syntax.html#character-references and https://infra.spec.whatwg.org/#control , the answer does not have these references. Also this shows across many PHP versions: https://3v4l.org/LXSS0g – hakre Dec 18 '19 at 11:41