Home >Backend Development >PHP Tutorial >How to add and remove backslashes before quotation marks in PHP
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)
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);//将字符串进行处理 }
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!