如何使用PHP實(shí)現(xiàn)javascript的escape和unescape函數(shù)_PHP教程
推薦:使用PHP會話(Session)實(shí)現(xiàn)用戶登陸功能對比起 Cookie,Session 是存儲在服務(wù)器端的會話,相對安全,并且不像 Cookie 那樣有存儲長度限制,本文簡單介紹 Session 的使用。 由于 Session 是以文本文件形式存儲在服務(wù)器端的,所以不怕客戶端修改 Session 內(nèi)容。實(shí)際上在服務(wù)器端的 Session 文件,PHP 自動修改
前端開發(fā)工程師都知道javascript有編碼函數(shù)escape()和對應(yīng)的解碼函數(shù)unescape(),而php中只有個urlencode和 urldecode,這個編碼和解碼函數(shù)對encodeURI和encodeURIComponent有效,但是對escape的是無效的。
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;
}
對應(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;
}
分享:解析php session_set_save_handler 函數(shù)的用法(mysql)本篇文章是對php中session_set_save_handler 函數(shù)的用法(mysql)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下 復(fù)制代碼 代碼如下: ?php /*============================文件說明======================================== @filename: session.class.php @description: 數(shù)據(jù)庫
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時間
- PHP中獎概率的抽獎算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- 如何用PHP獲取歌曲時間
- PHP實(shí)現(xiàn)上傳文件自動生成縮略圖加文字實(shí)例代碼
- 如何修改和添加Apache的默認(rèn)站點(diǎn)目錄
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- 在windows平臺上構(gòu)建自己的PHP實(shí)現(xiàn)方法(僅適用于php5.2)
- PHP技巧:優(yōu)化動態(tài)網(wǎng)頁技術(shù)PHP程序的12條技巧
- 新手入門:IIS6環(huán)境下的PHP最佳配置方法
- PHP教程之Ajax進(jìn)行Web開發(fā)
- 解讀ImageTTFText函數(shù)如何實(shí)現(xiàn)圖像加文字水印
- mysql to mssql,mssql 轉(zhuǎn)mysql轉(zhuǎn)換方法
- 相關(guān)鏈接:
復(fù)制本頁鏈接| 搜索如何使用PHP實(shí)現(xiàn)javascript的escape和unescape函數(shù)
- 教程說明:
PHP教程-如何使用PHP實(shí)現(xiàn)javascript的escape和unescape函數(shù)
。