function string2utf8($string){
$string = preg_replace_callback('/&#([0-9]+);/',create_function('$s','return dcr2utf8($s[1]);'),$string);
return preg_replace_callback('/&#x([a-f0-9]+);/i',create_function('$s','return dcr2utf8(hexdec($s[1]));'),$string);
}
function dcr2utf8($src){
$dest = '';
if($src <>> 6));
$dest .= chr(0x80 | ($src & 0x003f));
}elseif($src == 0xFEFF){
// nop -- zap the BOM
}elseif ($src >= 0xD800 && $src <= 0xDFFF){ // found a surrogate return false; }elseif($src <= 0xffff){ $dest .= chr(0xe0 | ($src >> 12));
$dest .= chr(0x80 | (($src >> 6) & 0x003f));
$dest .= chr(0x80 | ($src & 0x003f));
}elseif($src <= 0x10ffff){ $dest .= chr(0xf0 | ($src >> 18));
$dest .= chr(0x80 | (($src >> 12) & 0x3f));
$dest .= chr(0x80 | (($src >> 6) & 0x3f));
$dest .= chr(0x80 | ($src & 0x3f));
}else{
// out of range
return false;
}
return $dest;
}
Usage:
echo string2utf8("Vì Sao Chỉ Có Anh");
Source: http://annevankesteren.nl/2005/05/character-references
Title:
PHP Script to Convert Windows-1252 string to UTF-8
Description:
function string2utf8($string){ $string = preg_replace_callback('/&#([0-9]+);/',create_function('$s','return dcr2utf8...
...
Rating:
4