PHP handles HTML string __html

Source: Internet
Author: User
Tags html tags
when we develop the app interface, we often need to process HTML Rich text strings, with many useful functions in PHP for us to use functions you need to use:Str_replace () string replaces Html_entity_decode () HTML string to HTML tag strip_tags () removes all HTML tags preg_match_all () global regular match string Array_ Unique () to remove duplicate elements in an array (to solve the problem of duplication of elements caused by a global regular match) Array_flip () swaps the key value because the key name cannot be duplicated, so the repeating element is removed, and two times flip is able to get a distinct array of key values Array_ VALUES () returns an array with no key-value quickly remove HTML characters
$bewrite = Strip_tags (Html_entity_decode ($HTMLSTR), ' <br> ');

First turn the HTML tag and then remove the tag, but note that the text in the app still needs to be simply formatted.
And then we'll turn the BR tag back into the local app \ n

$bewrite = Str_replace (' <br/> ', ' \ n ', $bewrite);

But it seems that the string is still not clean, because there will be remnants of some of the HTML itself newline characters & and spaces, and then replace

$bewrite = Str_replace (' &nbsp; ', ', ', $bewrite);
$bewrite = Str_replace (' &amp; ', ', ', $bewrite);

But normally, our HTML string contains not only text, but also pictures, and the SRC attribute in img must be singled out. Regular matching tag attributes

Below I take an IMG match src picture Address for example

$unmatchedstr = Html_entity_decode ($data [' secondgoods_bewrite ']); Turn HTML string
preg_match_all ('/\]*>/i ', $unmatchedstr, $match);//Regular matching picture src address

But this is not finished, we found that the regular out of the link has many duplicate links, we have to remove the variable $match[1] duplicate values

$matches = Array_unique ($match [1]);
Two times Array_flip () may be more efficient than the unique one some
$matchresult = Array_values ($matches);//The value of the array of key values, remove the key name is complete

Enclosure:

For a remote SRC address, there is a special regular match that excludes local pictures.

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.