PHP Regular Expression implication

Source: Internet
Author: User
Meaning of the PHP regular expression
$string = Preg_replace ('/& (# (\d{3,5}|x[a-fa-f0-9]{4});)/', ' &\\1 ',
Str_replace (Array (' & ', ' ' ', ' < ', ' > '), Array (' & ', ' "', ' < ', ' > '), $string));

Escape special characters in a string

'/& ((# (\d{3,5}|x[a-fa-f0-9]{4}));)/', ' &\\1 ', which part of the meaning?


------Solution--------------------
Just disassemble and understand.
Then replace the & #nnnnn or & #xnnnn with & #nnnnn

\\1 indicates that the contents of the first parenthesis are preserved as-Is ((# (# (# (\d{3,5}
------Solution--------------------
X[A-FA-F0-9]{4})) This part
------Solution--------------------
References:
$string = preg_replace ('/&amp; (# (# (\d{3,5}
------Solution--------------------
X[A-FA-F0-9]{4}));)/', ' &\\1 ',
Str_replace (Array (' & ', ' ' ', ' < ', ' > '), Array (' &amp; ', ' &quot; ', ' &lt; ', ' &gt; '), $str
...

This is Discuz's filter function, right.
In fact, the consciousness of this function is to filter the HTML strings, but those that have already been filtered, such as "'" "single quotes double quotes." The "&" entity is & so like this is not filtered, reserved, in case of multiple filtering. The results & became &ampamp;




Canceling HTML code
function Shtmlspecialchars ($string) {
if (Is_array ($string)) {
foreach ($string as $key = = $val) {
$string [$key] = Shtmlspecialchars ($val);
}
} else{
$string = Preg_replace ('/& (# (# (\d{3,5}
------Solution--------------------
X[A-FA-F0-9]{4})
------Solution--------------------
[A-za-z] [A-z0-9] {1,5});) /', ' &\\1 ',
Str_replace (Array (' & ', ' ' ', ' < ', ' > ', ' \ '), Array (' & ', ' "', ' < ', ' > ', '), $string));
}
return $string;
}

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