Summary of php's methods for removing HTML tags from strings

Source: Internet
Author: User
Tags html tags

First, let's look at your own writing method.

The code is as follows: Copy code

Str_replace ("<div class = \" summary-text \ ">", '', str_replace ('</div> ','', str_replace ('</div>', '', $ vv )))

The simplest method is to replace the two divs specified in the $ vv variable. Later, we found a solution.

The code is as follows: Copy code

$ Info = strip_tags ($ vv );

All html tags are replaced. Let's look at the strip_tags function.

Trip_tags (string, allow): The function removes HTML, XML, and PHP labels.

The code is as follows: Copy code

$ Str = 'Guo bowl Basin-<span style = "color: # f00;"> PHP </span> ';
$ Str1 = strip_tags ($ str); // delete all HTML tags
$ Str2 = strip_tags ($ str, '<span>'); // retain the <span> tag
Echo $ str1; // output Guo bowl Basin-PHP
Echo $ str2; // different styles


As we can see above, some html tags can only be retained in PHP, but some html tags cannot be specified to be deleted. So I wrote a file for my usual lib Library.

The code is as follows: Copy code


/**
* Remove the specified HTML tag
*/
Function strip_only_tags ($ str, $ tags, $ stripContent = FALSE ){
$ Content = '';
     
If (! Is_array ($ tags )){
$ Tags = (strpos ($ str, '> ')! = False? Explode ('>', str_replace ('<', '', $ tags): array ($ tags ));
If (end ($ tags) = ''){
Array_pop ($ tags );
    }
  }
     
Foreach ($ tags as $ tag ){
If ($ stripContent ){
$ Content = '(. + <! -- '. $ Tag.' (--> | \ s [^>] *>) | )';
    }
     
$ Str = preg_replace ('# <! --? '. $ Tag.' (--> | \ s [^>] *>) '. $ content.' # is ', '', $ str );
  }
     
Return $ str;
}

Parameter description

$ Str-refers to a string to be filtered, such as div, p, em, img, and other html tags.
$ Tags-indicates that you want to remove a specified html tag, such as a, img, and p.
$ StripContent = FALSE-remove the content in the tag, such as deleting the entire link. The default value is False, that is, do not delete the content in the tag.

Instructions for Use

The code is as follows: Copy code

$ Target = strip_only_tags ($ source, array ('A', 'em ',' B '));

Removes tags a, em, and B from the $ source string.

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.