PHP escape and reverse literal string functions

Source: Internet
Author: User
In PHP, escape and inverse meanings of strings can also be implemented using the addslashes () and stripslashes () functions provided by PHP. In PHP, the escape and inverse meanings of strings can be implemented using the self-owned functions addslashes () and stripslashes () provided by PHP.

1. addslashes () function

The addslashes () function is used to add a backslash (\) to the specified string (\).

The syntax format is as follows:

addslashes(string);

Description: return string. a backslash is added before certain characters for database query statements. These characters are single quotation marks ('), double quotation marks ("), backslash (\), and NUL (NULL ).

2. stripslashes () function

The stripslashes () function is used to add a backslash (\) to the addcslashes () function to delete the original result.

The syntax format is as follows:

stripslashes(string);

We have introducedWhat is php escape and reverse literal string data?This section describes the escape character "\" and how to use the escape character to convert and reverse the meaning of the string, and describes how to use an instance. Here

Use the addslashes () function to escape the string, and then use the stripslashes () function to restore the string as follows:

 "; $ A = addslashes ($ str); // escape the string echo $ ."
"; // Output the escaped string $ B = stripslashes ($ a); // restore the escaped string by echo $ B ."
"; // Output the restored string?>

The output result is as follows:

This url_name = 'php Chinese net' this url_name = \ 'php Chinese net' \ 'This url_name = 'php Chinese net'


Note:Before data is inserted into the database, it is necessary to use the addcslashes () function to escape the string, so as to avoid errors when inserting the database without escaping special characters. In addition, for escape strings implemented using the addslashes () function, use the stripslashes () function to restore, but the data must be escaped again before it is inserted into the database.

Note:In a cached file, the addcslashes () function is used to escape the cached data values within a specified range.

When you want to input data to the database. For example, insert the name 'Reilly into the database, which requires escaping. Most databases use \ as the escape character: O \ 'Reilly. In this way, the data can be put into the database without inserting additional \. When the PHP command magic_quotes_sybase is set to on, it means that when 'is inserted,' is used for escape.

Addslashes escapes special characters in SQL statements, including ('), ("), (), (NUL, 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 magic_quotes_gpc is enabled by default before PHP5.3. it is mainly used to perform the addslashes operation on $ GET, $ POST, and $ cookies. Therefore, you do not need to repeatedly call addslashes on these variables, otherwise, it will be double escaping. However, magic_quotes_gpc has been deprecated in PHP5.3 and removed from PHP5.4. if you use the latest PHP version, do not worry about this problem. Stripslashes is the unescape function of addslashes.

The above are detailed descriptions of PHP escape and reverse literal string functions. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.