This article mainly shares with you how to enable json_encode to not automatically escape the slash "" in PHP. The article provides detailed sample code, which is of reference value to everyone, let's take a look.
Preface
Recently, when I saved the crawling link to the mysql database, I found that when I saved the link using json_encode, it showed escape characters in the database, and I didn't need this escape character, it looks unclear and occupies storage space.
Then it is found that, by default, the json format conversion using json_encode will automatically escape the string containing the slash in the data, but sometimes we do not need to escape them. This article describes how to use json_encode without automatically escaping the slash.
For the following array $ a, there are two solutions:
$a = array( 'http://www.baidu.com', 'http://www.baidu.com', 'http://www.baidu.com', 'http://www.baidu.com', 'http://www.baidu.com');
First, regular expression replacement:
$a = str_replace("\\/", "/", json_encode($a));var_dump($a);
Second, if the php version is 5.4 or later:
var_dump(json_encode($a,JSON_UNESCAPED_SLASHES));
For more articles about making json_encode not automatically escape the slash "/" in PHP, please follow the PHP Chinese website!