2014-01-15

php 把 HTML-ENTITIES HTML code (&#xxxxx;) 轉 utf8

function htmlentities2utf8($str){
$str = preg_replace_callback(
'/&#[0-9]+;/ismU'
,"____htmlentities2utf8"
,$str);
return $str;
}
function ____htmlentities2utf8($m){
return mb_convert_encoding($m[0], 'UTF-8', 'HTML-ENTITIES');
}

//下行要測試時 把 & 和 # 中的空格刪掉
echo htmlentities2utf8("何冠& #39053;") ;