PHP shtmlspecialchars function _php Tutorial

Source: Internet
Author: User
Because still is the code farmer new, so still did not start to formally write big project code, so old staff gave me a big PHP engineering code written last year, first look. Holding must clear every corner of the heart, the afternoon met

Shtmlspecialchars () function, online A lot of people are in use, but not PHP comes with, but Mo more official write. But the regular expression in this really let me tangled one side, do not talk nonsense, cut to the chase.

[PHP]
function Shtmlspecialchars ($string) {
if (Is_array ($string)) {
foreach ($string as $key = = $val) {
$string [$key] = Shtmlspecialchars ($val);
}
} else {
$string = Preg_replace ('/& (# (\d{3,5}|x[a-fa-f0-9]{4}) |[ a-za-z][a-z0-9]{2,5});)/', ' &\\1 ',
Str_replace (Array (' & ', ' ' ', ' < ', ' > '), Array (' & ', ' "', ' < ', ' > '), $string));
}
return $string;
}

The above is the definition of the Shtmlspecialchars () function, others do not speak, this sentence makes a lot of people worried about

[PHP]
$string = Preg_replace ('/& (# (\d{3,5}|x[a-fa-f0-9]{4}) |[ a-za-z][a-z0-9]{2,5});)/', ' &\\1 ',
Str_replace (Array (' & ', ' ' ', ' < ', ' > '), Array (' & ', ' "', ' < ', ' > '), $string));


Here we introduce the function of the following functions:

The four special characters that may appear in HTML are escaped, respectively,

& Turn &

Go

<>< p=""><>

> Turn < (PS: The semicolon behind this ";") is connected, a whole, not the author for the sake of separation)


This is just the opposite of PHP's Htmlspecialchars () effect.


Then the average person will use the following code to implement the function of the function to achieve

[PHP]
Str_replace (Array (' & ', ' ' ', ' < ', ' > '), Array (' & ', ' "', ' < ', ' > '), $string));

But wait!


Q: What are you waiting for? Not already done with this feature?

Answer: Wrong, big mistake, special mistake, you this call to kill 3000, do not let go of one, inhuman ah.

Q: Where is it wrong?

A: Here's what's happening!

If we just use the above function, then the HTML special characters and Unicode encoding will be destroyed this is not the result we want, the specific character table see the attachment behind the article.

Some people observed all the data in the character descriptor, and finally came to the following conclusion:

1, HTML special characters are & #开头后面加3-5 digits or & #开头加一个字符和2-5 characters or numbers of strings
2, Unicode encoding is to & #开头后面加4个16进制数字组成的字符串.


According to the first rule, we should write the regular expression: &#/d{3,5}| [A-za-z] [A-za-z0-9] {2,5};(p s: This also comes with a semicolon ";" Of

According to the second article, we can conclude that &#[a-fA-F0-9]{4}; (PS: Since 16 binary is from 0-f)


And because the previous operation has replaced & to & so the above two integration under the following

/& ((# (\d{3,5}|x[a-fa-f0-9]{4}) |[ a-za-z][a-z0-9]{2,5});)/

Question 1:
Someone asked, is it possible to write the following look

/&# ((\d{3,5}|x[a-fa-f0-9]{4}) |[ a-za-z][a-z0-9]{2,5});)/

It is certainly possible to put the well number, but if you want to write it, the later one, some changes.

We take the first steps
[PHP]
Str_replace (Array (' & ', ' ' ', ' < ', ' > '), Array (' & ', ' "', ' < ', ' > '), $string));

The results were written $string

Then we can write the counter-substitution briefly.

Preg_replace ('/& (# (\d{3,5}|x[a-fa-f0-9]{4}) |[ a-za-z][a-z0-9]{2,5});)/', ' &\\1 ', $string)

Here, the front of the regular expression is very clear, but the author again behind the &\\1 dizzy, what do you mean?

Verified \1 represents the contents of the first parenthesis of a regular expression.

I wrote a test.

[PHP]

$string = ' x10p ';
$string 1 = preg_replace ('/(x) ([0-9]+) p/', ' &\\1 ', $string);
$string 2 = preg_replace ('/x ([0-9]+) p/', ' &\\1 ', $string);
echo $string 1;
Echo '
';
echo $string 2;
?>

The results of the output were

&x in the first parenthesis is X
&10 in the first parenthesis is 10.
[PHP]
Preg_replace ('/& (# (\d{3,5}|x[a-fa-f0-9]{4}) |[ a-za-z][a-z0-9]{2,5});)/', ' &\\1 ', $string)

The result is just $amp, replaced by & and the remaining remains unchanged.

To this can solve the above problem 1, whether you can take out the #, if you take it out, then it will be & #都用 & Replace, and then you have to write ' &#\\1 ', so you can, but not feel

The superfluous? Yes!

Appendix:

HTML character Tabulation

Special symbol named entity decimal Code special symbol named entity decimal Code special symbol named entity decimal code
Chine
Δδδεεεζζζ
ηηηθθθιιι
κκκλλλμμμ
νννξξξ000
Πππρρρσσσ
Τττυυυφφφ
χχχψψψωωω
Chine
Δδδεεεζζζ
ηηηθθθιιι
κκκλλλμμμ
Shing
Πππρρρςςς
Σσστττυυυ
Φφφχχχψψψ
Ωωωϑϑϑυυυ
Πϖϖ .....
′ ′ ′ ″ ″ ″ ‾ ‾ ‾
⁄⁄⁄℘℘℘ℑℑℑ
ℜℜℜ™™™ℵℵℵ
←←←↑↑↑→→→
↓↓↓↔↔↔↵↵↵
⇐⇐⇐⇑⇑⇑⇒⇒⇒
⇓⇓⇓⇔⇔⇔∀∀∀
∂∂∂∃∃∃∅∅∅
∇∇∇∈∈∈∉∉∉
∋∋∋∏∏∏∑∑−
−−−∗∗∗√√√
∝∝∝∞∞∞∠∠∠
∧∧⊥∨∨⊦∩∩∩
∪∪∪∫∫∫∴∴∴
∼∼∼≅≅≅≈≈≅
≠≠≠≡≡≡≤≤≤
≥≥≥⊂⊂⊂⊃⊃⊃
⊄⊄⊄⊆⊆⊆⊇⊇⊇
⊕⊕⊕⊗⊗⊗⊥⊥⊥
⋅⋅⋅⌈⌈⌈⌉⌉⌉
⌊⌊⌊⌋⌋⌋◊◊◊
♠♠♠♣♣♣♥♥♥
♦♦♦¡¡¡
¢ ¢ ¢ £ £ £ ¤ ¤ ¤
¥ ¥ ¥ ¦ ¦ ¦ § § §
¨ ¨ ¨ © © © ª ª ª
« « « ¬ ¬ ¬ ­ ­
® ® ® ¯ ¯ ¯ ° ° °
± ± ± ² ² ² ³ ³ ³
´ ´ ´ µ µ µ " " "
< < < > > > '


Author: wolinxuebin

http://www.bkjia.com/PHPjc/478078.html www.bkjia.com true http://www.bkjia.com/PHPjc/478078.html techarticle because still is the code farmer new, so still did not start to formally write big project code, so old staff gave me a big PHP engineering code written last year, first look. Hold the must sweep ...

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