Home > Backend Development > PHP Tutorial > How to add and remove backslashes before quotation marks in PHP

How to add and remove backslashes before quotation marks in PHP

藏色散人
Release: 2023-04-07 18:04:01
forward
3353 people have browsed it

In order to prevent injection or overflow, the PHP program automatically adds a backslash "" in front of double quotes, single quotes, backslashes, and NULL through the PHP instruction magic_quotes_gpc.

The default PHP command magic_quotes_gpc is on, that is, it is turned on. At this time, you can use the stripslashes() function to remove the automatically added backslashes.

Usage:

stripslashes($str)
Copy after login

The output result is to remove the backslash.

If the local PHP command magic_quotes_gpc is off, if this function is used, the normal backslashes will be removed.

The solution is to use the function get_magic_quotes_gpc() to detect. If it is in the open state, then the backslash is removed. If it is in the closed state, the backslash is not removed.

The program code is as follows:

$str=$_POST["str"];//读取str的内容赋值给$str变量
if(get_magic_quotes_gpc())//如果get_magic_quotes_gpc()是打开的
{
$str=stripslashes($str);//将字符串进行处理
}
Copy after login

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to add and remove backslashes before quotation marks in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template