Converting numeric character references to unicode

Input

y s ƌ ” µ છ x ‘ Ε Μ Ƭ


Output

Unchanged: y s ƌ ” µ છ x ‘ Ε Μ Ƭ

Codepoints: 121 115 396 8221 181 2715 120 8216 917 924 428

In utf-8: y s ƌ ” µ છ x ‘ Ε Μ Ƭ


Test it out

Input:
Output:
[No input]

Source:

Highlighted with my highlighter.

<?php

function stringToCodePoints($str) {
 $str = fixCharacters($str);
 $str = preg_replace_callback(
 '/&#([0-9]+);/',
 create_function(
 '$s',
 'return $s[1];'
 ),
 $str
 );
 $str = preg_replace_callback(
 '/&#x([a-f0-9]+);/i',
 create_function(
 '$s',
 'return hexdec($s[1]);'
 ),
 $str
 );
 return $str;
}
function stringToUtf8($str) {
 $str = fixCharacters($str);
 $str = preg_replace_callback(
 '/&#([0-9]+);/',
 create_function(
 '$s',
 'return code2utf($s[1]);'
 ),
 $str
 );
 $str = preg_replace_callback(
 '/&#x([a-f0-9]+);/i',
 create_function(
 '$s',
 'return code2utf(hexdec($s[1]));'
 ),
 $str
 );
 return $str;
}
function fixCharacters($str) {
 $r = array(
 '&#128;' => '&#8364;',
 '&#129;' => '',
 '&#130;' => '&#8218;',
 '&#131;' => '&#402;',
 '&#132;' => '&#8222;',
 '&#133;' => '&#8230;',
 '&#134;' => '&#8224;',
 '&#135;' => '&#8225;',
 '&#136;' => '&#710;',
 '&#137;' => '&#8240;',
 '&#138;' => '&#352;',
 '&#139;' => '&#8249;',
 '&#140;' => '&#338;',
 '&#141;' => '',
 '&#142;' => '&#381;',
 '&#143;' => '',
 '&#144;' => '',
 '&#145;' => '&#8216;',
 '&#146;' => '&#8217;',
 '&#147;' => '&#8220;',
 '&#148;' => '&#8221;',
 '&#149;' => '&#8226;',
 '&#150;' => '&#8211;',
 '&#151;' => '&#8212;',
 '&#152;' => '&#732;',
 '&#153;' => '&#8482;',
 '&#154;' => '&#353;',
 '&#155;' => '&#8250;',
 '&#156;' => '&#339;',
 '&#157;' => '',
 '&#158;' => '&#382;',
 '&#159;' => '&#376;'
 );
 return strtr($str, $r);
}
// From php.net:
function code2utf($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 '';
}

?>