Say less nonsense, directly on the code:
$str = '<p>
I need to keep the text </p><p> <br/></p><p> alsdkjf;laskjd;flasj;</p><p> ASDJFASJD; Lkafsj;lfdkj</p><p> aksdjfl;ak</p>‘;$subject=Strip_tags($str);//Remove HTML Tags$pattern= '/\s/';//Remove whitespace$content=Preg_replace($pattern, ‘‘,$subject);
//Note that, if the interception of Chinese characters, mb_substr need to add UTF-8 parameters, otherwise it will appear garbled
EchoMB_SUBSTR ($content, 0, +, "UTF-8");//interception of 40 kanji
Here, mention the difference between the htmlspecialchars function and the strip_tags function:
$str= "<a href= ' http://www.manongjc.com ' > Yard course ' \" </a> "; Echo Htmlspecialchars ($str); Echo "<br/><br/>"; Echo Strip_tags ($str);
The results of the browser output:
Review the elements you will find:
From the results we can see that the difference between htmlspecialchars () and Strip_tags is as follows:
Difference One:
The strip_tags function uses to remove HTML tags, and htmlspecialchars does not remove HTML tags, just converts them to HTML instances, So the biggest difference between the two is the deletion of HTML tags, and the conversion of HTML tags to other characters.
Difference Two:
If you need to remove HTML tags in the string inside the label is wrong, for example, less than the symbol, the use of the strip_tags function will return the error, and Htmlspecialchars will not have errors appear, is still converted to an HTML entity.
Difference Three:
It is generally recommended to use the htmlspecialchars function when preventing XSS attacks, because strip_tags can delete HTML tags, but it does not remove "或‘。因此就算你使用了
strip_tags, You still need to use the htmlspecialchars function to filter out"或‘
In the form submission or user message board, if you want the raw output of the data with the browser, then use the htmlspecialchars function, do not use the strip_tags function.
In addition, the test found a very interesting question, in the database stored in the value of the content is this:
$str = '<p> <BR/></p><p> <img src="/upload/image/2018-02-09/20180209132853_70302.gif" alt="" /> </p><p style="color: #6C6969; font-family:&quot;font-size:14px;text-align: Justify;background-color: #FFFFFF;"> Peng really exhausted, he practiced for eight months, almost every day for more than 10 hours of gymnastics, in order to maintain eight ABS body, he only boiled food, without any seasoning. </p><p style="color: #6C6969; font-family:&quot;font-size:14px;text-align: Justify;background-color: #FFFFFF;"> <BR/></p>';
If you need to remove the HTML tags, you need to use the htmlspecialchars_decode function, use the strip_tags function to remove the HTML tags.
PHP intercepts article content generates a plain text digest (removes HTML tags)