php自定義函數(shù)實現(xiàn)JS的escape的方法示例
更新時間:2016年07月07日 10:20:18 作者:HTL
這篇文章主要介紹了php自定義函數(shù)實現(xiàn)JS的escape的方法,結合完整實例形式分析了php實現(xiàn)JS的escape功能函數(shù)的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了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; }
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《php字符串(string)用法總結》、《PHP數(shù)組(Array)操作技巧大全》、《php排序算法總結》、《PHP常用遍歷算法與技巧總結》、《PHP數(shù)據(jù)結構與算法教程》、《php程序設計算法總結》、《PHP數(shù)學運算技巧總結》、《php正則表達式用法總結》、《PHP運算與運算符用法總結》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:
- php中的路徑問題與set_include_path使用介紹
- PHP include_path設置技巧分享
- PHP中spl_autoload_register()函數(shù)用法實例詳解
- PHP中FTP相關函數(shù)小結
- 全面解析PHP操作Memcache基本函數(shù)
- php的debug相關函數(shù)用法示例
- php中array_column函數(shù)簡單實現(xiàn)方法
- PHP中Array相關函數(shù)簡介
- PHP與Java對比學習日期時間函數(shù)
- 淺談PHP eval()函數(shù)定義和用法
- PHP 在數(shù)組中搜索給定的簡單實例 array_search 函數(shù)
- 淺談PHP檢查數(shù)組中是否存在某個值 in_array 函數(shù)
- PHP中set_include_path()函數(shù)相關用法分析