String strip_tags (string str [, string allowable_tags])
Disadvantages:
This function can only retain the desired html tag, that is, the string allowable_tags parameter.
In the yizero comment, I know other usage of the allowable_tags parameter of this function.
The code is as follows: |
Copy code |
Strip_tags ($ source, "); remove the html tag. Strip_tags ($ source, '<div> <em>'); retains the div, img, and em tags in the string. |
If you want to remove the specified html tag. Then this function cannot meet the requirements. So I used this function.
The code is as follows: |
Copy code |
<? Php /** * Delete the specified HTML tag and its content. Currently, only single Tag cleanup is supported. * * @ Param string $ string -- string to be processed * @ Param string $ tagname -- Name of the tag to be deleted * @ Param boolean $ clear -- whether to delete tag content * @ Return string -- return the processed string */ Function replace_html_tag ($ string, $ tagname, $ clear = false ){ $ Re = $ clear? '': '1 '; $ SC = '/<'. $ tagname .'(? : S [^>] *)?> ([SS] *?)? </'. $ Tagname.'>/I '; Return preg_replace ($ SC, $ re, $ string ); } |
The following is the test code.
The code is as follows: |
Copy code |
// Baidu homepage content $ String = file_get_contents ('http: // www.111cn.net /'); // Remove the style and content $ String = replace_html_tag ($ string, 'style', true ); $ String = replace_html_tag ($ string, 'script', true ); // Remove Tag a and save the content $ String = replace_html_tag ($ string, 'A '); // Remove the span tag and save the content $ String = replace_html_tag ($ string, 'span '); Echo $ string; ?> |
If we want to delete data between the specified two
The code is as follows: |
Copy code |
<? Php /** * Remove specific html tags from PHP * @ Param array $ string * @ Param bool $ str * @ Return string */ Function _ strip_tags ($ tagsArr, $ str ){ Foreach ($ tagsArr as $ tag ){ $ P [] = "/(<(? :/". $ Tag." | ". $ tag.") [^>] *>)/I "; } $ Return_str = preg_replace ($ p, "", $ str ); Return $ return_str; } $ Str = "<B> hello </B> <input type = 'text' name =''/> <a href = 'http: // www.baidu.com '> Baidu, you will know </a> "; Echo _ strip_tags (array ("B", "input", "a"), $ str); # Remove the B and INPUT tags ?>
|