如何使用PHP實(shí)現(xiàn)javascript的escape和unescape函數(shù)
更新時(shí)間:2013年06月29日 09:50:50 作者:
本篇文章是對(duì)使用PHP實(shí)現(xiàn)javascript的escape和unescape函數(shù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
前端開(kāi)發(fā)工程師都知道javascript有編碼函數(shù)escape()和對(duì)應(yīng)的解碼函數(shù)unescape(),而php中只有個(gè)urlencode和urldecode,這個(gè)編碼和解碼函數(shù)對(duì)encodeURI和encodeURIComponent有效,但是對(duì)escape的是無(wú)效的。
javascript中的escape()函數(shù)和unescape()函數(shù)用戶字符串編碼,類似于PHP中的urlencode()函數(shù),下面是php實(shí)現(xiàn)的escape函數(shù)代碼:
/**
* js escape php 實(shí)現(xiàn)
* @param $string the sting want to be escaped
* @param $in_encoding
* @param $out_encoding
*/
function escape($string, $in_encoding = 'UTF-8',$out_encoding = 'UCS-2') {
$return = '';
if (function_exists('mb_get_info')) {
for($x = 0; $x < mb_strlen ( $string, $in_encoding ); $x ++) {
$str = mb_substr ( $string, $x, 1, $in_encoding );
if (strlen ( $str ) > 1) { // 多字節(jié)字符
$return .= '%u' . strtoupper ( bin2hex ( mb_convert_encoding ( $str, $out_encoding, $in_encoding ) ) );
} else {
$return .= '%' . strtoupper ( bin2hex ( $str ) );
}
}
}
return $return;
}
對(duì)應(yīng)的解碼php unescape代碼是:
function unescape($str)
{
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i ++)
{
if ($str[$i] == '%' && $str[$i + 1] == 'u')
{
$val = hexdec(substr($str, $i + 2, 4));
if ($val < 0x7f)
$ret .= chr($val);
else
if ($val < 0x800)
$ret .= chr(0xc0 | ($val >> 6)) .
chr(0x80 | ($val & 0x3f));
else
$ret .= chr(0xe0 | ($val >> 12)) .
chr(0x80 | (($val >> 6) & 0x3f)) .
chr(0x80 | ($val & 0x3f));
$i += 5;
} else
if ($str[$i] == '%')
{
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
} else
$ret .= $str[$i];
}
return $ret;
}
javascript中的escape()函數(shù)和unescape()函數(shù)用戶字符串編碼,類似于PHP中的urlencode()函數(shù),下面是php實(shí)現(xiàn)的escape函數(shù)代碼:
復(fù)制代碼 代碼如下:
/**
* js escape php 實(shí)現(xiàn)
* @param $string the sting want to be escaped
* @param $in_encoding
* @param $out_encoding
*/
function escape($string, $in_encoding = 'UTF-8',$out_encoding = 'UCS-2') {
$return = '';
if (function_exists('mb_get_info')) {
for($x = 0; $x < mb_strlen ( $string, $in_encoding ); $x ++) {
$str = mb_substr ( $string, $x, 1, $in_encoding );
if (strlen ( $str ) > 1) { // 多字節(jié)字符
$return .= '%u' . strtoupper ( bin2hex ( mb_convert_encoding ( $str, $out_encoding, $in_encoding ) ) );
} else {
$return .= '%' . strtoupper ( bin2hex ( $str ) );
}
}
}
return $return;
}
對(duì)應(yīng)的解碼php unescape代碼是:
復(fù)制代碼 代碼如下:
function unescape($str)
{
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i ++)
{
if ($str[$i] == '%' && $str[$i + 1] == 'u')
{
$val = hexdec(substr($str, $i + 2, 4));
if ($val < 0x7f)
$ret .= chr($val);
else
if ($val < 0x800)
$ret .= chr(0xc0 | ($val >> 6)) .
chr(0x80 | ($val & 0x3f));
else
$ret .= chr(0xe0 | ($val >> 12)) .
chr(0x80 | (($val >> 6) & 0x3f)) .
chr(0x80 | ($val & 0x3f));
$i += 5;
} else
if ($str[$i] == '%')
{
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
} else
$ret .= $str[$i];
}
return $ret;
}
相關(guān)文章
PHP使用new StdClass()創(chuàng)建空對(duì)象的方法分析
這篇文章主要介紹了PHP使用new StdClass()創(chuàng)建空對(duì)象的方法,結(jié)合具體實(shí)例形式分析了php空對(duì)象的創(chuàng)建與使用方法,需要的朋友可以參考下2017-06-06
使用ltrace工具跟蹤PHP庫(kù)函數(shù)調(diào)用的方法
這篇文章主要介紹了使用ltrace工具跟蹤PHP庫(kù)函數(shù)調(diào)用的方法,結(jié)合實(shí)例形式分析了ltrace工具用來(lái)跟蹤PHP庫(kù)函數(shù)運(yùn)行時(shí)間的相關(guān)技巧,需要的朋友可以參考下2016-04-04
與文件上傳有關(guān)的php配置參數(shù)總結(jié)
搞個(gè)了圖片上傳,死活不好使,后來(lái)發(fā)現(xiàn)是php參數(shù)配置的問(wèn)題,下面總結(jié)下與文件上傳有關(guān)的php參數(shù),有類似情況的朋友可以參考下哈2013-06-06
PHP編程實(shí)現(xiàn)微信企業(yè)向用戶付款的方法示例
這篇文章主要介紹了PHP編程實(shí)現(xiàn)微信企業(yè)向用戶付款的方法,涉及php針對(duì)微信接口調(diào)用、配置及交互操作相關(guān)技巧,需要的朋友可以參考下2017-07-07
php使用strpos判斷字符串中數(shù)字類型子字符串出錯(cuò)的解決方法
這篇文章主要介紹了php使用strpos判斷字符串中數(shù)字類型子字符串出錯(cuò)的解決方法,結(jié)合具體問(wèn)題分析了strpos函數(shù)針對(duì)數(shù)字類型子字符串進(jìn)行判斷時(shí)的注意事項(xiàng)及類型轉(zhuǎn)換處理技巧,需要的朋友可以參考下2017-04-04
snoopy PHP版的網(wǎng)絡(luò)客戶端提供本地下載
snoopy是非常不錯(cuò)的模仿網(wǎng)絡(luò)客戶端的php類,但提供本地下載的很少,國(guó)外的下載又麻煩而且又慢,所以我弄到本站方便大家下載2008-04-04

