php讓json_encode不自動(dòng)轉(zhuǎn)義斜杠“/”的方法
hp中怎么讓json_encode不自動(dòng)轉(zhuǎn)義斜杠“/”?下面本篇文章給大家介紹一下PHP中讓json_encode不自動(dòng)轉(zhuǎn)義斜杠“/”的方法。
最近將使用爬蟲爬取的鏈接保存到 mysql 數(shù)據(jù)庫中時(shí),發(fā)現(xiàn)我將鏈接使用 json_encode 保存時(shí)候,在數(shù)據(jù)庫中卻顯示了轉(zhuǎn)義字符,我并不需要這轉(zhuǎn)義的,看起來不清晰而且占用存儲(chǔ)空間。
后來發(fā)現(xiàn)在默認(rèn)的情況之下使用 json_encode 對數(shù)組進(jìn)行 json 格式的轉(zhuǎn)換時(shí)候會(huì)自動(dòng)的將數(shù)據(jù)中含有斜杠的字符串進(jìn)行轉(zhuǎn)義,但是我們往往有的時(shí)候不需要藥對它們進(jìn)行轉(zhuǎn)義的,本文說說如何使用 json_encode 不自動(dòng)轉(zhuǎn)義斜杠。
對于如下數(shù)組 $a,現(xiàn)有兩種辦法解決:
$a = array( ’http://www.baidu.com’, ’http://www.baidu.com’, ’http://www.baidu.com’, ’http://www.baidu.com’, ’http://www.baidu.com’);
其一,正則替換:
$a = str_replace('/', '/', json_encode($a));var_dump($a);
其二,若 php 版本是 5.4 及以上的話:
var_dump(json_encode($a,JSON_UNESCAPED_SLASHES));
到此這篇關(guān)于php讓json_encode不自動(dòng)轉(zhuǎn)義斜杠“/”的方法的文章就介紹到這了,更多相關(guān)php怎么讓json_encode不自動(dòng)轉(zhuǎn)義斜杠“/”內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
