Home  >  Article  >  Backend Development  >  Detailed explanation of PHP escape and anti-escape string functions

Detailed explanation of PHP escape and anti-escape string functions

伊谢尔伦
伊谢尔伦Original
2018-05-12 11:31:1717984browse

In the PHP language, string escaping and anti-escaping can be achieved using the own functions addslashes() and stripslashes() provided by PHP.

1. addslashes() function

addslashes() function is used to add backslashes (\) to the specified string string.

The syntax format is as follows:

addslashes(string);

Description: Returns a string with backslashes added before certain characters for database query statements, etc. These characters are single quotes ('), double quotes ("), backslash (\) and NUL (NULL character).

2. stripslashes() function

# The ##stripslashes() function is used to add the backslash (\) to the addcslashes() function and return it to the original state.

The syntax format is as follows:

stripslashes(string);

We introduced

## earlier. #What is php escaping and anti-escaping string data, introduces the escape character "\", and how to use the transfer character to escape and anti-escape strings, and uses examples Explained. HereThe example of using addslashes() function to escape the string and then using stripslashes() function to restore it is as follows:

<?php
  $str = "this url_name = &#39;PHP中文网&#39;";
  echo $str."<br>";
  $a = addslashes($str);               //对字符串进行转义
  echo $a."<br>";                           //输出转义后的字符串
  $b = stripslashes($a);                //对转义后的字符串进行还原
  echo $b."<br>";                          //输出还原后的字符串
?>

The output result is as follows:

this url_name = &#39;PHP中文网&#39;
this url_name = \&#39;PHP中文网\&#39;
this url_name = &#39;PHP中文网&#39;

Note:

Before inserting data into the database, it is necessary to use the addcslashes() function to escape the string to prevent special characters from being inserted into the database without escaping An error occurs. In addition, the escaped strings implemented using the addslashes() function are restored using the stripslashes() function, but the data must be escaped again before being inserted into the database.

Note:

In the cache file, the addcslashes() function is generally used to escape the value of cache dataWhen data is to be entered into the database, for example, the name. O'reilly is inserted into the database, which requires escaping it. Most databases use \ as the escape character: O\'reilly. This allows the data to be inserted into the database without inserting extra \. When the directive magic_quotes_sybase is set to on, it means that ' will be used to escape when inserting '.

addslashes escapes special characters in SQL statements, including ('), ("), () , (NUL) four characters. This function is used when the DBMS does not have its own escape function. However, if the DBMS has its own escape function, it is recommended to use the original function. For example, MySQL has the mysql_real_escape_string function to escape SQL. Note that before PHP5.3, magic_quotes_gpc was enabled by default, which mainly performed addslashes operations on $GET, $POST, and $COOKIE, so there is no need to repeatedly call addslashes on these variables, otherwise double escaping will occur. However, magic_quotes_gpc has been abandoned in PHP5.3 and has been removed since PHP5.4. If you use the latest version of PHP, you don't have to worry about this problem. stripslashes is the unescape function of addslashes.

The above is the detailed content of Detailed explanation of PHP escape and anti-escape string functions. 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