PHP intercepts article content generates a plain text digest (removes HTML tags)

Source: Internet
Author: User

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 = '&lt;p&gt;  &LT;BR/&gt;&lt;/p&gt;&lt;p&gt;  &lt;img src=&quot;/upload/image/2018-02-09/20180209132853_70302.gif&quot; alt=&quot;&quot; /&gt; &lt;/p&gt;&lt;p style=&quot;color: #6C6969; font-family:&amp;quot;font-size:14px;text-align: Justify;background-color: #FFFFFF;&quot;&gt;  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. &lt;/p&gt;&lt;p style=&quot;color: #6C6969; font-family:&amp;quot;font-size:14px;text-align: Justify;background-color: #FFFFFF;&quot;&gt;  &LT;BR/&gt;&lt;/p&gt;';

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)

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.