Workaround for PHP to use Strip_tags () to remove HTML tags and still have whitespace

Source: Internet
Author: User
This article mainly introduces PHP using Strip_tags () to remove HTML tags still have a blank solution, involving PHP regular replacement of the relevant operating skills, the need for friends can refer to. We hope to help you.

Specific as follows:

$subject = Strip_tags ($newsRs [' content ']);//Remove HTML tag $pattern = '/\s/';//Remove Blank $content = preg_replace ($pattern, ', $ subject);      $seodata [' articledescription '] = mb_substr ($content, 0, 80);//intercept 80 characters

Note: Strip_tags () in the first step; just replace the HTML tag with a space

Like what:

<p>  Hello </p><p>  <br/></p><p>  alsdkjf;laskjd;flasj;</p>< p>  asdjfasjd;lkafsj;lfdkj</p><p>  aksdjfl;ak</p>

Strip_tags () after processing becomes

  Hello,      empty line  ... ALSDKJF;LASKJD;FLASJ;      Empty line  ... asdjfasjd;lkafsj;lfdkj      empty line  ... Aksdjfl;ak

This may not be what we want, and then we'll just remove the blanks with a regular replacement.

Final Result:

Hello, Alsdkjf;laskjd;flasj;asdjfasjd;lkafsj;lfdkjaksdjfl;ak.

In addition: \s denotes whitespace characters, \s denotes non-whitespace characters (note case, general case indicates the opposite meaning)

Related recommendations:

A detailed description of the regular expression capturing group and the non-capturing group of PHP

Basic and simple examples of PHP regular expressions

Summary of common functions in PHP

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.