Php removes the specified html tag and content

Source: Internet
Author: User
Tags html tags tagname

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
?>

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.