fork download
  1. <?php
  2.  
  3. function html_character_reference_decode($string, $encoding='UTF-8', $fixMappingBug=true) {
  4. $deref = create_function('$match', '
  5. if (strtolower($match[1][0]) === "x") {
  6. $codepoint = intval(substr($match[1], 1), 16);
  7. } else {
  8. $codepoint = intval($match[1], 10);
  9. }
  10. // @see http://w...content-available-to-author-only...t.fi/~jkorpela/www/windows-chars.html
  11. if ($fixMappingBug && $codepoint >= 130 && $codepoint <= 159) {
  12. $mapping = array(
  13. 8218, 402, 8222, 8230, 8224, 8225, 710, 8240, 352, 8249,
  14. 338, 141, 142, 143, 144, 8216, 8217, 8220, 8221, 8226,
  15. 8211, 8212, 732, 8482, 353, 8250, 339, 157, 158, 376);
  16. $codepoint = $mapping[$codepoint-130];
  17. }
  18. var_dump($codepoint);
  19. return mb_convert_encoding(pack("N", $codepoint), $encoding, "UTF-32BE");
  20. ');
  21. return preg_replace_callback('/&#(x[0-9a-f]+|[0-9]+);/i', $deref, $string);
  22. }
  23.  
  24. var_dump(html_character_reference_decode('&#151;'));
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
int(151)
string(1) "�"