Example of removing json backslash in php

Example of removing json backslash in php

1. Remove backslashes through the "stripslashes($_POST['json']);" method.

2. Use "json_decode" to decode the string in JSON format.

The json string passed to PHP via AJAX is sometimes escaped with a backslash "\". When processing it in PHP, you need to remove the backslash first and then json_decode it.

$str = stripslashes($_POST['json']);$arr = json_decode($str,true);

stripslashes() function: Removes backslashes added by the addslashes() function.

json_decode: Decodes a string in JSON format.

Knowledge point expansion:

How to prevent json_encode from automatically escaping slashes "/" in PHP

Recently, when I saved the links crawled by the crawler into the mysql database, I found that when I saved the links using json_encode, the escape characters were displayed in the database. I don’t need this escape, it looks unclear and takes up storage space.

Later, I found that under the default circumstances, when using json_encode to convert the array to json format, the strings containing slashes in the data will be automatically escaped, but sometimes we don't need to escape them. This article explains how to use json_encode without automatically escaping slashes.

For the following array $a, there are two solutions:

$a = array(
 '//www.jb51.net,
 '//www.jb51.net,
 '//www.jb51.net,
 '//www.jb51.net,
 '//www.jb51.net
);

First, regular replacement:

$a = str_replace("\\/", "/", json_encode($a));
var_dump($a);

Second, if the PHP version is 5.4 and above:

var_dump(json_encode($a,JSON_UNESCAPED_SLASHES));

This is the end of this article about the example of how to remove backslashes in json in php. For more information about how to remove backslashes in json in php, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • PHP removes the backslash \ in the json string and removes the backslash before the double quotes

<<:  calc() to achieve full screen background fixed width content

>>:  Web front-end development CSS related team collaboration

Recommend

HTML line spacing setting methods and problems

To set the line spacing of <p></p>, us...

Web Design Tutorial (4): About Materials and Expressions

<br />Previous Web Design Tutorial: Web Desi...

Specific example of MySQL multi-table query

1. Use the SELECT clause to query multiple tables...

Analysis of MySQL data backup and recovery implementation methods

This article uses examples to describe how to bac...

jQuery implements form validation function

jQuery form validation example / including userna...

Example of how to reference environment variables in Docker Compose

In a project, you often need to use environment v...

How to use limit_req_zone in Nginx to limit the access to the same IP

Nginx can use the limit_req_zone directive of the...

This article takes you to explore NULL in MySQL

Table of contents Preface NULL in MySQL 2 NULL oc...

HTML multi-header table code

1. Multi-header table code Copy code The code is a...

vue dynamic component

Table of contents 1. Component 2. keep-alive 2.1 ...

How to deal with garbled characters in Mysql database

In MySQL, database garbled characters can general...