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