php自定義函數(shù)實(shí)現(xiàn)JS的escape的方法示例
本文實(shí)例講述了php自定義函數(shù)實(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程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php正則表達(dá)式用法總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php中的路徑問題與set_include_path使用介紹
- PHP include_path設(shè)置技巧分享
- PHP中spl_autoload_register()函數(shù)用法實(shí)例詳解
- PHP中FTP相關(guān)函數(shù)小結(jié)
- 全面解析PHP操作Memcache基本函數(shù)
- php的debug相關(guān)函數(shù)用法示例
- php中array_column函數(shù)簡(jiǎn)單實(shí)現(xiàn)方法
- PHP中Array相關(guān)函數(shù)簡(jiǎn)介
- PHP與Java對(duì)比學(xué)習(xí)日期時(shí)間函數(shù)
- 淺談PHP eval()函數(shù)定義和用法
- PHP 在數(shù)組中搜索給定的簡(jiǎn)單實(shí)例 array_search 函數(shù)
- 淺談PHP檢查數(shù)組中是否存在某個(gè)值 in_array 函數(shù)
- PHP中set_include_path()函數(shù)相關(guān)用法分析
相關(guān)文章
php常用數(shù)組函數(shù)實(shí)例小結(jié)
這篇文章主要介紹了php常用數(shù)組函數(shù),結(jié)合實(shí)例形式總結(jié)分析了php常用數(shù)組函數(shù)array_merge、array_slice及array_map的功能與使用技巧,需要的朋友可以參考下2016-12-12PHP回調(diào)函數(shù)概念與用法實(shí)例分析
這篇文章主要介紹了PHP回調(diào)函數(shù)概念與用法,簡(jiǎn)單介紹了回調(diào)函數(shù)的概念、原理,并結(jié)合實(shí)例形式分析了回調(diào)函數(shù)的相關(guān)使用技巧,需要的朋友可以參考下2017-11-11php將圖片文件轉(zhuǎn)換成二進(jìn)制輸出的方法
這篇文章主要介紹了php將圖片文件轉(zhuǎn)換成二進(jìn)制輸出的方法,涉及php針對(duì)圖片文件的讀取與轉(zhuǎn)換輸出技巧,需要的朋友可以參考下2015-06-06PHP實(shí)現(xiàn)多個(gè)關(guān)鍵詞搜索查詢功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)多個(gè)關(guān)鍵詞搜索查詢功能,結(jié)合實(shí)例形式分析了php使用mysql中l(wèi)ike、union等語句模糊查詢相關(guān)操作技巧,需要的朋友可以參考下2018-06-06php定期拉取數(shù)據(jù)對(duì)比方法實(shí)例
這篇文章主要給大家介紹了關(guān)于php定期拉取數(shù)據(jù)對(duì)比的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用php具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09PHP數(shù)組與字符串互相轉(zhuǎn)換實(shí)例
在本篇文章里小編給大家分享的是關(guān)于PHP數(shù)組與字符串互相轉(zhuǎn)換實(shí)例內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2020-05-05