Home > Article > Backend Development > How to use addslashes() function in PHP? (code example)
The
addslashes() function is a built-in function of PHP, which can add backslashes before predefined characters in a string. The following article will introduce to you the usage of PHP addslashes() function. I hope it will be helpful to you. [Video tutorial recommendation: PHP tutorial]
PHP addslashes() function
addslashes() function Is a built-in function in PHP that returns an escaped string with a backslash in front of a predefined character. Can
Note: It will not use any of the specified characters in the parameters.
The predefined characters are:
● Single quotation mark (')
● Double quotation mark (")
● Reverse Slash (\)
● Empty (null) value
Basic syntax:
addslashes($string)
Parameters: The addslashes() function only accepts one parameter $string, which specifies the input string that needs to be escaped. We can also say that this parameter specifies a string in which we can add backslash before the predefined characters bar.
Return value: Returns an escaped string with backslashes, and the backslashes will be added in front of the predefined characters.
PHP addslashes() function usage example
The following is a code example to see how the addslashes() function is used.
Example 1:
<?php // 输入字符串 $str = addslashes('这是一个字母"p"。'); // 输出 echo($str); ?>
Output:
这是一个字母\"p\"。
Example 2:
<?php // 输入字符串 $str = addslashes("It's a dream!"); // 输出 echo($str); ?>
Output
It\'s a dream!
Description: The addslashes() function is one of two functions that are often used to prevent injection attacks. The other function is the htmlspecialchars() function. Both functions escape special characters.
The above is the detailed content of How to use addslashes() function in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!