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

Monitor the size change of a DOM element through iframe

A common problem encountered during the developme...

Introduction to Kubernetes (k8s)

I had always wanted to learn Kubernetes because i...

Detailed explanation of the setting of background-image attribute in HTML

When it comes to pictures, the first thing we thi...

How to install MySql in CentOS 8 and allow remote connections

Download and install. First check whether there i...

Detailed tutorial on building a local idea activation server

Preface The blogger uses the idea IDE. Because th...

Use of Linux stat command

1. Command Introduction The stat command is used ...

The perfect solution for forgetting the password in mysql8.0.19

Recommended reading: MySQL 8.0.19 supports accoun...

Do you know how to use mock in vue project?

Table of contents first step: The second step is ...

Detailed explanation of persistent storage of redis under docker

In this chapter, we will start to operate redis i...

Detailed explanation of JS ES6 variable destructuring assignment

Table of contents 1. What is deconstruction? 2. A...

Practice of using SuperMap in Vue

Table of contents Preface Related Materials Vue p...

Linux beginners in virtual machines configure IP and restart the network

For those who are new to virtual machines or have...