Home > Backend Development > PHP Tutorial > Detailed explanation of php addslashes() function and stripslashes() function examples

Detailed explanation of php addslashes() function and stripslashes() function examples

怪我咯
Release: 2023-03-07 20:06:02
Original
2050 people have browsed it

What are the functions of the addslashes() function and stripslashes() function?

addslashes(): Add backslashes before certain predefined characters in the input string. This processing is for the needs of database query statements, etc. These predefined characters are: single quote ('), double quote ("), backslash (\), NULL.

stripslashes(): Remove the additions added by the addslashes() function backslash. This function is used to clean the data retrieved from the database or HTML form (if there are two consecutive backslashes, remove one and keep one; if there is only one backslash, remove it directly.)

ps: By default, the PHP command magic_quotes_gpc is on and addslashes() is automatically run on all GET, POST and COOKIE data. Do not use addslashes() on strings that have been escaped by magic_quotes_gpc. , because this will cause double-level escaping. When encountering this situation, you can use the function get_magic_quotes_gpc() for detection. Example:

if (get_magic_quotes_gpc()){
code....
}

Understanding their functions, let’s take a look at their syntax:

addslashes() function: before certain predefined characters in the input string Add backslash

Syntax:

addslashes(string)
Copy after login

Parameter details:

string Specifies the characters to be escaped String.

Example

The following example adds a backslash to the predefined characters in the string. The code is as follows:

<?php
$str = "Who&#39;s Peter Griffin?";
echo $str . " This is not safe in a database query.<br>";
echo addslashes($str) . " This is safe in a database query.";
?>
Copy after login

Code operation Result:

Detailed explanation of php addslashes() function and stripslashes() function examples


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

##Syntax

stripslashes(string)
Copy after login
Parameter details:

string Specifies the string to be checked. Example

Remove the backslash, the code is as follows

<?php
echo stripslashes("Who\&#39;s Bill Gates?");
?>
Copy after login
Code running result:

[PHP video tutorial recommendation]:

Detailed explanation of php addslashes() function and stripslashes() function examplesphp.cn Dugu Jiujian (4)-php video tutorial

The above is the detailed content of Detailed explanation of php addslashes() function and stripslashes() function examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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