PHP string escape functions (addslashes, stripslashes)

Source: Internet
Author: User

In PHP, there are two functions related to the escape of strings: addslashes and stripslashes.

Addslashes ($ string), which is used to add a backslash (\) before a specified predefined character to prepare a suitable string for the string stored in the database and the database query statement.

Note: by default, the magic_quotes_gpc command of PHP is on, and addslashes () is automatically run for all GET, POST, and COOKIE data (). Do not use addslashes () for strings that have been escaped by magic_quotes_gpc, because this causes double-layer escape. In this case, you can use the get_magic_quotes_gpc () function for detection.

Stripslashes ($ string) is the inverse function of addslashes (). It is used to delete the backslash added by the addslashes () function to restore the characters to be escaped. It is also called inverse meaning, it is mainly used to clear data retrieved from a database or HTML form.

Which characters will addslashes escape:

Single quotes (')
Double quotation marks (")
Backslash (\)
NULL

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

Single quotes (')
Backslash (\)

Strings with double quotation marks as delimiters support the following escape characters:

\ N line feed (LF or ASCII character 0x0A (10 ))
\ R press enter (CR or ASCII character 0x0D (13 ))
\ T horizontal tab (HT or ASCII character 0x09 (9 ))
\ Backslash
\ $ Dollar sign
\ "Double quotation marks
\ [0-7] {} The regular expression sequence matches a character represented by the octal symbol
\ X [0-9A-Fa-f] {} This regular expression matches a sequence of characters represented by a hexadecimal symbol

Articles you may be interested in
  • Php string replacement function str_replace is faster than preg_replace
  • Summary of String Functions in PHP
  • Php performance optimization: Using isset () to determine the string length is faster than strlen ()
  • Comparison and Analysis of string encoding functions escape, encodeURI, and encodeURIComponent in javascript
  • Some powerful string processing functions forgotten by php
  • Php string Filter Function
  • Php's most precise String Length truncation Function
  • PHP generates continuous numbers (letters) Array Function range () analysis, PHP lottery program function

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.