News新闻

业界新闻动态、技术前沿
Who are we?

您的位置:首页      乐道系统FAQ      用PHP将Unicode 转化为UTF-8的实现方法(推荐)

用PHP将Unicode 转化为UTF-8的实现方法(推荐)

发布日期:2017-02-08 00:00:00 81

实例如下:

function unescape($str) {
  $str = rawurldecode($str);
  preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r);
  $ar = $r[0];
  //print_r($ar);
  foreach($ar as $k=>$v) {
    if(substr($v,0,2) == "%u"){
      $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,-4)));
 }
    elseif(substr($v,0,3) == "&#x"){
      $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,3,-1)));
 }
    elseif(substr($v,0,2) == "&#") {
       
      $ar[$k] = iconv("UCS-2BE","UTF-8",pack("n",substr($v,2,-1)));
    }
  }
  return join("",$ar);
}
echo unescape("紫星蓝");