PHP中如何unicode編碼,在JavaScript中h如何解碼
PHP中進行unicode編碼后,在JavaScript中如何解碼?js中h這樣的,怎么轉(zhuǎn)碼?
php代碼示例
http://www.dbjr.com.cn/article/1.htm字符串被編碼成:https://www.jb51.net/article/1.htm
$str='http://www.dbjr.com.cn/article/1.htm'; echo UnicodeEncode($str); echo unicodeDecode(UnicodeEncode($str)); function UnicodeEncode($str){ //編碼 preg_match_all('/./u',$str,$matches); $unicodeStr = ""; foreach($matches[0] as $m){ $unicodeStr .= "&#".base_convert(bin2hex(iconv('UTF-8',"UCS-4",$m)),16,10); } return $unicodeStr; } function unicodeDecode($unicode_str){ //解碼 $json = '{"str":"'.$unicode_str.'"}'; $arr = json_decode($json,true); if(empty($arr)) return ''; return $arr['str']; }
JavaScript中如何解碼示例
瀏覽器控制臺輸出http://www.dbjr.com.cn/article/1.htm
function unt(str) { return str.replace(/&#(x)?([^&]{1,5});?/g, function (a, b, c) { return String.fromCharCode(parseInt(c, b ? 16 : 10)); }) } //帶;號 var str="https://www.jb51.net/article/1.htm"; //不帶分號 var str2="https://www.jb51.net/article/1.htm"; console.log(unt(str)); console.log(unt(str2));
到此這篇關(guān)于PHP中如何unicode編碼,在JavaScript中&#104如何解碼的文章就介紹到這了,更多相關(guān)PHP中unicode編碼,JavaScript中解碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript中數(shù)組與對象的使用方法區(qū)別
數(shù)組(array)是按次序排列的一組值。JS其實沒有真正的數(shù)組,只是用對象模擬數(shù)組。本質(zhì)上,數(shù)組屬于一種特殊的對象。typeof運算符會返回數(shù)組的類型是object。在javascript中,數(shù)組又可以認為是索引數(shù)組,即可以用整數(shù)來進行索引。數(shù)組和對象在這種情況下非常接近。2022-12-12微信小程序開發(fā)中var that =this的用法詳解
這篇文章主要介紹了微信小程序開發(fā)中var that =this的用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01兼容FireFox 用javascript寫的一個畫圖函數(shù)
兼容FireFox 用javascript寫的一個畫圖函數(shù)...2007-08-08