escape 編碼
發表於 : 2005-11-07 15:26:32
http://www.neo.com.tw/archives/000152.html
使用 PHP 解譯 javascript escape() 編碼過的字串
PHP 跟 Javascript 都有 HTML encoding 的函式,編碼過的結果雖然類似,但是還是有些微的不同,所以不能互轉。
二者的差異可以可參考:
http://web.archive.org/web/200308111812 ... vbscript_1
節錄如下:
PHP:
urlencode( ) All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.
urldecode( )All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.
Javascript:
escape(str) All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding
那要如何用 PHP 來讀取 escape() 編碼過的字串呢?
可以用以下的方式來讀取 (範例為 Unicode 解譯為 big5)
修改自:http://web.archive.org/web/200410312258 ... itemid=100
$str = uniDecode($str,'big-5');
function uniDecode($str,$charcode){
$text = preg_replace_callback("/%u[0-9A-Za-z]{4}/",toUtf8,$str);
return mb_convert_encoding($text, $charcode, 'utf-8');
}
function toUtf8($ar){
foreach($ar as $val){
$val = intval(substr($val,2),16);
if($val < 0x7F){ // 0000-007F
$c .= chr($val);
}elseif($val < 0x800) { // 0080-0800
$c .= chr(0xC0 | ($val / 64));
$c .= chr(0x80 | ($val % 64));
}else{ // 0800-FFFF
$c .= chr(0xE0 | (($val / 64) / 64));
$c .= chr(0x80 | (($val / 64) % 64));
$c .= chr(0x80 | ($val % 64));
}
}
return $c;
}
由 Neo 發表於 April 29, 2004 03:17 PM | 引用 (0) |
使用 PHP 解譯 javascript escape() 編碼過的字串
PHP 跟 Javascript 都有 HTML encoding 的函式,編碼過的結果雖然類似,但是還是有些微的不同,所以不能互轉。
二者的差異可以可參考:
http://web.archive.org/web/200308111812 ... vbscript_1
節錄如下:
PHP:
urlencode( ) All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.
urldecode( )All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.
Javascript:
escape(str) All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding
那要如何用 PHP 來讀取 escape() 編碼過的字串呢?
可以用以下的方式來讀取 (範例為 Unicode 解譯為 big5)
修改自:http://web.archive.org/web/200410312258 ... itemid=100
$str = uniDecode($str,'big-5');
function uniDecode($str,$charcode){
$text = preg_replace_callback("/%u[0-9A-Za-z]{4}/",toUtf8,$str);
return mb_convert_encoding($text, $charcode, 'utf-8');
}
function toUtf8($ar){
foreach($ar as $val){
$val = intval(substr($val,2),16);
if($val < 0x7F){ // 0000-007F
$c .= chr($val);
}elseif($val < 0x800) { // 0080-0800
$c .= chr(0xC0 | ($val / 64));
$c .= chr(0x80 | ($val % 64));
}else{ // 0800-FFFF
$c .= chr(0xE0 | (($val / 64) / 64));
$c .= chr(0x80 | (($val / 64) % 64));
$c .= chr(0x80 | ($val % 64));
}
}
return $c;
}
由 Neo 發表於 April 29, 2004 03:17 PM | 引用 (0) |