Escape string for PHP

Source: Internet
Author: User
Tags ord regular expression

Today came across a deal with a special character of the file, and again notice the problem in PHP:

* PHP strings with single quotes as delimiters, supporting two escape ' and \
* a PHP string with double quotes as delimiters that supports the following escapes:
N line-Wrapping (LF or ASCII character 0x0A (10))
R carriage return (CR or ASCII character 0x0D (13))
T horizontal tab (HT or ASCII character 0x09 (9))
\ Backslash
$ dollar Sign
"Double quotes
[0-7] {1,3} This regular expression sequence matches a character represented by a octal symbol
x[0-9a-fa-f]{1,2} This regular expression sequence matches a character represented in hexadecimal notation

Give a few examples:

An example that contains special characters:

$str = "FFFFFFFF";
Echo (strlen ($STR));
Echo ("n");
For ($i =0 $i <strlen ($STR); $i + +) echo ("T". Ord ($str {$i}));
Echo ("n");

Output results:
----------------------

9
102 102 102 102 0 102 102 102 102

Example of replacing special characters

$str = "FFFFFFFF";
$str = Str_replace ("x0", "", $str);
or use $STR = Str_replace ("", "", $str);
or with $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 results:
----------------------
8
102 102 102 102 102 102 102 102


Octal ASCII code example:

//Note that a string that conforms to the regular [0-7]{1,3} represents an octal ASCII code.
$str = "1237101188"; ///Here 8 does not meet the requirements, is amended to "\8 " (ASCII is 92 and a)
Echo strlen ($str ));
Echo ("n");
for ($i =0; $i <strlen ($STR); $i + +) echo ("T". Ord ($str {$i}

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.