Make json_encode not automatically escape the slash "/" in PHP

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.