php自定義函數(shù)實現(xiàn)JS的escape的方法示例
本文實例講述了php自定義函數(shù)實現(xiàn)JS的escape的方法。分享給大家供大家參考,具體如下:
//php function
function escape($string) {
$n = $bn = $tn = 0;
$output = '';
$special = "-_.+@/*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
while($n < strlen($string)) {
$ascii = ord($string[$n]);
if($ascii == 9 || $ascii == 10 || (32 <= $ascii && $ascii <= 126)) {
$tn = 1;$n++;
} elseif(194 <= $ascii && $ascii <= 223) {
$tn = 2;$n += 2;
} elseif(224 <= $ascii && $ascii <= 239) {
$tn = 3;$n += 3;
} elseif(240 <= $ascii && $ascii <= 247) {
$tn = 4;$n += 4;
} elseif(248 <= $ascii && $ascii <= 251) {
$tn = 5;$n += 5;
} elseif($ascii == 252 || $ascii == 253) {
$tn = 6;$n += 6;
} else {
$n++;
}
$singleStr = substr($string,$bn,$tn);
$charVal = bin2hex(iconv('utf-8', 'ucs-2', $singleStr));
if(base_convert($charVal, 16, 10) > 0xff) {
if (!preg_match("/win/i", PHP_OS))
$charVal = substr($charVal, 2, 2).substr($charVal, 0, 2);
$output .= '%u' . $charVal;
}
else {
if(false !== strpos($special, $singleStr))
$output .= $singleStr;
else
$output .="%" . dechex(ord($string[$bn]));
}
$bn = $n;
}
return $output;
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php排序算法總結(jié)》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設計算法總結(jié)》、《PHP數(shù)學運算技巧總結(jié)》、《php正則表達式用法總結(jié)》、《PHP運算與運算符用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
- php中的路徑問題與set_include_path使用介紹
- PHP include_path設置技巧分享
- PHP中spl_autoload_register()函數(shù)用法實例詳解
- PHP中FTP相關(guān)函數(shù)小結(jié)
- 全面解析PHP操作Memcache基本函數(shù)
- php的debug相關(guān)函數(shù)用法示例
- php中array_column函數(shù)簡單實現(xiàn)方法
- PHP中Array相關(guān)函數(shù)簡介
- PHP與Java對比學習日期時間函數(shù)
- 淺談PHP eval()函數(shù)定義和用法
- PHP 在數(shù)組中搜索給定的簡單實例 array_search 函數(shù)
- 淺談PHP檢查數(shù)組中是否存在某個值 in_array 函數(shù)
- PHP中set_include_path()函數(shù)相關(guān)用法分析
相關(guān)文章
PHP對象轉(zhuǎn)換為數(shù)組函數(shù)(遞歸方法)
本方法主要是應用于迭代對象。我應用的地方是simplexml中的simplexml_load_string()上,因為返回的全是對象,如果提取數(shù)據(jù)比較麻煩,所以應用了下面的函數(shù)2012-02-02

