Detailed explanation of PHP string escape functions (addslashes, stripslashes)_PHP tutorial

WBOY
Release: 2016-07-13 10:31:21
Original
1242 people have browsed it

In PHP, there are two functions related to string escaping, they are addslashes and stripslashes.

addslashes($string), adds a backslash () before the specified predefined characters, used to prepare appropriate strings for strings stored in the database and database query statements.

Note: By default, the PHP directive magic_quotes_gpc is on, automatically running addslashes() on all GET, POST and COOKIE data. Do not use addslashes() on strings that have been escaped by magic_quotes_gpc, as this will result in double escaping. When encountering this situation, you can use the function get_magic_quotes_gpc() to detect it.

stripslashes($string) is the inverse function of addslashes(). It is used to delete the backslashes added by the addslashes() function to restore the escaped characters. It is also called anti-escaping. It is mainly used to clean up the database. Or the data retrieved from the HTML form.

So which characters will be escaped by addslashes, as follows:

Single quote (')
Double quotes (")
backslash ()
NULL

In addition, strings with single quotes as delimiters support two escape characters:

Single quote (')
backslash ()

A string delimited by double quotes, supports the following escapes:

n Line feed (LF or ASCII character 0x0A (10))
r carriage return (CR or ASCII character 0x0D (13))
t horizontal tab character (HT or ASCII character 0x09 (9))
\ backslash
$ dollar sign
"Double quotes
[0-7]{1,3} This regular expression sequence matches a character represented in octal notation
x[0-9A-Fa-f]{1,2} This regular expression sequence matches a character represented in hexadecimal notation

Articles you may be interested in

  • php string replacement function str_replace is faster than preg_replace
  • String processing functions in PHP (String Functions) Full summary
  • php performance optimization: use isset() to determine the string length speed Faster than strlen()
  • Comparison and analysis of JavaScript’s string encoding functions escape, encodeURI, encodeURIComponent
  • Some of the forgotten and powerful string processing functions of PHP
  • PHP filter string function
  • PHP’s most accurate string length interception function
  • PHP generates continuous number (letter) array function range() analysis, PHP lottery program function

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764092.htmlTechArticleIn PHP, there are two functions related to string escaping, they are addslashes and stripslashes. addslashes($string), add a backslash () before the specified predefined characters, use...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!