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 (' ', ', ', $bewrite);
$bewrite = Str_replace (' & ', ', ', $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.