PHP Escape Character Issues

Source: Internet
Author: User
on the internet, searched a lot of articles, above all said:
PHP string with single quote delimiter, supports 2 escapes: \ \, \ '
PHP string with double quotation marks, supports 8 escapes: \ \, \ ", \ r, \ n, \ t, \$, \[0-7]{1,3}, \x[0-9a-fa-f]{1,2}
But in practice, this is not the case, the single and double quotes, addshlashes () will be escaped, the following example is evidence, is there any disagreement?
Here are the test results:
$single _quote_str = ' double quotation mark ' inside the ' single quote ' Test. ';
$double _quote_str = "\" double quotation mark \ "Inside of the ' single quotation mark ' Test.";

Single Quote test:
The original output string = ' single quotation mark ' within the ' double quotation mark ' test.
Escape string =\ ' single quotation mark \ ' inside of \ ' double quotation mark \ ' Test.
Restore string = "double quotation mark" test in ' single quotation mark '.
Double quote test:
Original Output string = ' single quote ' Test in ' double quotation marks '.
Escape string =\ "double quotation mark \" Inside the \ ' single quotation mark \ ' Test.
Restore string = ' single quotation mark ' test in ' double quotation marks '.


Reply to discussion (solution)

And, uh. If in single quotation marks: \ and \ \ After Addslashes (), the same 2 backslashes will be turned: \ \.

Addslashes was forced to escape!

2 floor, obviously did not understand my meaning.
The addslashes is escaped for a specific escape character.
Only 2 are supported in single quotes: \ \, \ "
But actually for \ ' also support, you take a closer look at my test results.

You have to understand why there is an escape.
When single quotes are enclosed in single quotation marks, such as ' AB ' C '
When double quotation marks are enclosed in double-quoted strings, such as "AB" C "
Did you say that the string ended at B, or did it end in C?
Can't you figure it out? This is because the ' (') ambiguity caused ambiguity
So in order to solve this ambiguity, the concept of escaping was introduced. The character \ Generally does not appear in the body, so it is used as an escape character
' Ab\ ' C ', ' ab\ ' C ', ', ', ' causes the quotation marks to lose the meaning of quotation marks, but only as ordinary characters
\ r, \ n, \ t causes the letter R, N, the tee to lose the meaning of its letter, and is used as a carriage return, newline, tab for ASCII code

Although it is seldom seen in the text, it is still possible to appear. So just use \ \ To lose the escape function

String addslashes (String str)


Returns a string that is preceded by a backslash in order for the database query statement to be preceded by some characters. These characters are single quotes ('), double quotation marks ("), backslashes (\), and NUL (the NULL character).

The escaped string uses addslashes () because it causes a double-layer escape,
You can use the function GET_MAGIC_QUOTES_GPC () to detect this situation.

Haha, the single quotation mark inside has a \, or two \, after Addslashes (), are two \ \, do not know this count is not a bug?
$single _quote_str = ' 1 backslashes: \; 2 backslashes: \ \ ';
Display Word string = 1 backslash: \; 2 backslashes: \
Escape string = 1 backslashes: \ \; 2 backslash: \ \
Restore string = 1 backslashes: \; 2 backslashes: \

  • 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.