熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

PHP中的UNICODE 編碼與解碼

2022-06-13   來源: PHP編程 
本篇文章是對PHP中的UNICODE 編碼與解碼進行了詳細的分析介紹需要的朋友參考下  

  方法一:

復制代碼 代碼如下:
<?php
function unicode_encode($name)
{
$name = iconv(UTF UCS $name);
$len = strlen($name);
$str = ;
for ($i = ; $i < $len ; $i = $i + )
{
$c = $name[$i];
$c = $name[$i + ];
if (ord($c) > )
{ //兩個字節的文字
$str = ubase_convert(ord($c) )str_pad(base_convert(ord($c) ) STR_PAD_LEFT);
}
else
{
$str = $c;
}
}
return $str;
}
//將UNICODE編碼後的內容進行解碼
function unicode_decode($name)
{
//轉換編碼將Unicode編碼轉換成可以浏覽的utf編碼
$pattern = /([w]+)|(u([w]{}))/i;
preg_match_all($pattern $name $matches);
if (!empty($matches))
{
$name = ;
for ($j = ; $j < count($matches[]); $j++)
{
$str = $matches[][$j];
if (strpos($str u) === )
{
$code = base_convert(substr($str ) );
$code = base_convert(substr($str ) );
$c = chr($code)chr($code);
$c = iconv(UCS UTF $c);
$name = $c;
}
else
{
$name = $str;
}
}
}
return $name;
}

  
方法二:

復制代碼 代碼如下:

  
function unicodeutf($str){
if(!$str) return $str;
$decode = json_decode($str);
if($decode) return $decode;
$str = [" $str "];
$decode = json_decode($str);
if(count($decode) == ){
return $decode[];
}
return $str;
}


From:http://tw.wingwit.com/Article/program/PHP/201311/21149.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.