Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of php slash automatic escape

Shulou Source: shulou.com Published: 2022-06-02 07:56:39 10月05日 Update

This article introduces the relevant knowledge of "how to solve php slash automatic escape". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Php slash automatic escape solution: 1, through str_replace regular replacement;2, use "json_encode($a,JSON_UNESCAPED_SLASHES)" statement.

Operating environment: Windows 7, PHP 7.4, DELL G3

What about php slashes?

PHP json_encode does not automatically escape slash "/" method

Recently, when I saved links crawled by crawler to mysql database, I found that when I saved links with json_encode, the escape characters were displayed in the database. I didn't need this escape character. It looked unclear and took up storage space.

Later, it was found that by default, when json_encode is used to convert an array to json format, it will automatically escape strings containing slashes in the data, but we often do not need to escape them. This article talks about how to use json_encode not to automatically escape slashes.

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 replacement:

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

Second, if PHP version 5.4 and above:

var_dump(json_encode($a,JSON_UNESCAPED_SLASHES)); The content of "php slash automatic escape" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Escape slash data time content method character situation database array more regular knowledge link practical learned next dilemma string actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS MySQL NVidia Docker