Home  >  Article  >  Backend Development  >  How to use addslashes() function in PHP? (code example)

How to use addslashes() function in PHP? (code example)

青灯夜游
青灯夜游Original
2019-04-22 13:55:473578browse

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]

How to use addslashes() function in PHP? (code example)

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(&#39;这是一个字母"p"。&#39;);  
  
// 输出
echo($str);  
?>

Output:

这是一个字母\"p\"。

Example 2:

<?php  
// 输入字符串
$str = addslashes("It&#39;s a dream!");  
  
// 输出
echo($str);  
?>

Output

It\&#39;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!

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