PHP escape string

Source: Internet
Author: User

Today, I encountered a special character processing problem. I noticed this problem again in PHP:

* A PHP string with single quotes as the separator. Two escape characters/'and // are supported //
* A PHP string with double quotation marks as the delimiter. The following escape characters are supported:
/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] {1, 3} 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

For example:

An example with/0 special characters is as follows:

$ STR = "FFFF/0 ffff ";
Echo (strlen ($ Str ));
Echo ("/N ");
For ($ I = 0; $ I <strlen ($ Str); $ I ++) echo ("/t". ord ($ STR {$ I }));
Echo ("/N ");

Output result:
----------------------

9
102 102 102 102 0 102 102 102

Example of replacing special characters

$ STR = "FFFF/0 ffff ";
$ STR = str_replace ("/x0", "", $ Str );
// Or use $ STR = str_replace ("/0", "", $ Str );
// Or use $ STR = str_replace (CHR (0), "", $ Str );
Echo (strlen ($ Str ));
Echo ("/N ");
For ($ I = 0; $ I <strlen ($ Str); $ I ++) echo ("/t". ord ($ STR {$ I }));
Echo ("/N ");
Output result:
----------------------
8
102 102 102 102 102 102 102

Octal ASCII code example:

// Note that the string that matches the regular expression/[0-7] {1, 3} represents an octal ASCII code.
$ STR = "/0/01/02/3/7/10/011/08/8"; // The/8 here does not meet the requirements, corrected to "// 8" (ASCII: 92 and 56)
Echo (strlen ($ Str ));
Echo ("/N ");
For ($ I = 0; $ I <strlen ($ Str); $ I ++) echo ("/t". ord ($ STR {$ I }));
Echo ("/N ");
Output result:
----------------------
11
0 1 2 3 7 8 9 0 56 92 56

Hexadecimal ASCII code example:

$ STR = "/X0/x1/x2/X3/X7/X8/x9/x10/X11/xFF ";
Echo (strlen ($ Str ));
Echo ("/N ");
For ($ I = 0; $ I <strlen ($ Str); $ I ++) echo ("/t". ord ($ STR {$ I }));
Echo ("/N ");
Output result:
----------------------
10
0 1 2 3 7 8 9 16 17 255

 

Related Article

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.