PHP escape characters

Source: Internet
Author: User
PHP escape characters have been found on the Internet and many articles have been found, as described above:
Php string with single quotes as the delimiter. two escape characters are supported :\\,\'
Php strings with double quotation marks as delimiters. eight escape characters are supported: \, \ ", \ r, \ n, \ t, \ $, \ [0-7] {1, 3}, \ x [0-9A-Fa-f] {1, 2}
However, in practice, this is not the case. single quotation marks and double quotation marks, addshlashes () are all escaped. in the following example, do you think differently?
The test results are as follows:
$ Single_quote_str = '\' double quotation marks in single quotation marks \ 'test .';
$ Double_quote_str = "\" double quotation marks \ "test the 'single quote .";

Single quotation mark test:
Test the "double quotation marks" in the original output string = 'single quota.
Escape string = \ 'single quotes \' \ "double quotation marks \" test.
Test the function of restoring the double quotation marks in the string = 'single quota.
Double quotation mark test:
Test the 'single quota' in the original output string = "double quotation marks.
Escape string = \ "double quotation marks \" \ 'single quotation marks \ 'test.
Test the 'single quota' function in the restored string = "double quotation marks.


Reply to discussion (solution)

Also. If '\' and '\' are enclosed by addslashes () in single quotes, they are converted into the same two backslash :\\.

Addslashes is forced to escape!

The second floor clearly did not understand what I meant.
Addslashes is used to escape specific escape characters.
Only two single quotes are supported :\\,\"
But it actually supports \ '. you should take a closer look at my test results.

First, you need to understand why there is an escape
When a string enclosed in single quotes contains single quotes, such as 'AB' c'
When a string enclosed by double quotation marks contains double quotation marks, such as "AB" c"
Do you say that the string ends at B or ends at c?
Can't you figure it out? This is because the ambiguity is caused by the ambiguity '(").
To solve this ambiguity, the concept of escape is introduced. Character \ generally does not appear in the body, so it is used as an escape character.
'AB \ 'C', "AB \" c ", \', \" make the quotation marks lose the meaning of the quotation marks, and are only treated as common characters
\ R, \ n, \ t make the letters r, n, and t lose the meaning of their letters, and are used as ascii code carriage return, line feed, tab

Although \ rarely appears in the body, it is still possible. So we can use \ to make it useless.

String addslashes (string str)


Returns a string that requires a backslash before certain characters for database query statements. These characters are single quotation marks ('), double quotation marks ("), backslash (\), and NUL (NULL ).

The escaped string uses addslashes (), because this causes double-layer escape,
In this case, you can use the get_magic_quotes_gpc () function for detection.

Haha, there is a \ or two \ in a single quotation mark. after addslashes (), both are \. I don't know if this is a bug?
$ Single_quote_str = '1 Backslash: \; 2 Backslash :\\';
Display string = 1 Backslash: \; 2 Backslash :\
Escape string = 1 Backslash: \; 2 Backslash :\\
Restore string = 1 Backslash: \; 2 Backslash :\

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.