Summarize how PHP deletes content inside HTML tags and tags

Source: Internet
Author: User

Often grilled others site articles of the pits;
I mean the kind of bulk collection that doesn't look at the content;
The function of removing HTML tags is always used;
Here are 3 different methods of use;

$str=‘<div><p>这里是p标签</p><a href="">这里是a标签</a><br></div>‘;
Phpcopy

1: Delete all or keep the specified HTML tag
PHP comes with a function strip_tags to meet the requirements,
How to use:
Strip_tags (String,allow);
String: Strings that need to be processed;
Allow: You need to keep the specified label, you can write more than one;

echo strip_tags($str,‘<p><a>‘);//输出:<p>这里是p标签</p><a href="">这里是a标签</a>
Phpcopy

The advantage of this function is that it is simple and rude;
But the shortcomings are also obvious;
If there are a lot of labels;
And I just want to delete the specified one;
Write a lot of labels that need to be kept;
So there's a second way;

2: Delete the specified HTML tag
Method of Use: Strip_html_tags ($tags, $STR);
$tags: Label to delete (array format)
$STR: The string to be processed;

functionStrip_html_tags($tags,$str){$html=Array();Foreach($tagsAs$tag){$html[]="/(< (?: \ /".$tag."|".$tag.") [^>]*>)/I";}$data=Preg_replace($html,‘‘,$str); return $data;} echo strip_html_tags(array(' P ',' img '),$str);  Output:<div> here is P tag <a href= "" > here is a label </a><br></div>;       
Phpcopy

3: Delete the contents of tags and labels
Method of Use: Strip_html_tags ($tags, $STR);
$tags: Label to delete (array format)
$STR: The string to be processed;

functionStrip_html_tags($tags,$str){$html=Array();Foreach($tagsAs$tag){$html[]='/< '.$tag.'. *?>[\s|\s]*?<\/'.$tag.' >/';$html[]='/< '.$tag.'. *?>/';}$data=Preg_replace($html,‘‘, $STR ) return  $data }echo  Strip_html_tags (array  ' a ' ,) , $STR ; Output <div><p> here is P tag </p><br></div>       
Phpcopy

Many website articles will bring the website name and link;
such as <a href= "http://www.baijunyao.com" > Bai Jun Haruka blog </a>;
This function is the tyranny of this;
Do not take this function to collect this station AH;
Or promise not to kill you;
4: The ultimate function, delete the specified label, delete or retain the contents of the tag;
Method of Use: Strip_html_tags ($tags, $str, $content);
$tags: Label to delete (array format)
$STR: The string to be processed;
$ontent: Whether to delete content within a label 0 preserve content 1 do not preserve content

/** * Delete the specified label * * @param array $tags deleted array form * @param string $str HTML String * @param bool $content true Preserve label contents t EXT * @return Mixed * *functionStriphtmltags($tags,$str,$content=True){$html=[];Whether to keep the text character inside the labelIf($content){Foreach($tagsAs$tag){$html[]='/(< '.$tag.' .*?> (. | \ n) *?<\/'.$tag.' >)/is ';}}Else{Foreach($tagsAs$tag){$html[]="/(< (?: \ /".$tag."|".$tag. ") [^>]*>)/is "; } } $data = preg_replace($html, ", $str); return $data;} //Output <div><p> here is the P tag </p><br></div>;   
Phpcopy

The front pulls so much;
In fact the last function is dry;
Take a breath to get rid of all kinds of tags removed difficult diseases do not bother;
Don't look below this one;
Nothing but a little color to look good;
I mainly take the picture as the cover of the article;

Summarize how PHP deletes content inside HTML tags and 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.