Summary of PHP methods for removing specified HTML tags

Source: Internet
Author: User

In php, the most common HTML tag can be replaced by the strip_tags function, which can be used to filter all html tags, next I will introduce other methods besides this function.

Sometimes we need to store html tabs in the database, but in some cases we need to take pure data without html tags. At this time, we need to process the data with html tags, remove all html tags. Htmlspecialchars () is usually used to filter html, but the html characters are escaped. The html source code is displayed, and the html tag can be removed using strip_tags.


The default function of PHP removes the specified html Tag named strip_tags, which is useful in some scenarios.

Strip_tags


Strip_tags-Strip HTML and PHP tags from a string


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.

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

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.

$ Source = '<div> <a href = "" target = "_ blank"> This a example from <em> lixiphp </em> </a> <strong>! </Strong> </div>
';
 
$ Target = strip_only_tags ($ source, array ('A', 'em '));
 
// Target results
// <Div> This a example from <strong>! </Strong> </div>

Other methods

The Code is as follows: Copy code


<? Php
// Retrieve the br mark
Function strip ($ str)
{
$ Str = str_replace ("<br>", "", $ str );
// $ Str = htmlspecialchars ($ str );
Return strip_tags ($ str );
}
?>


A custom function

/

The Code is as follows: Copy code
**
* Retrieve html tags
*
* @ Access public
* @ Param string str
* @ Return string
*
*/
Function deletehtml ($ str ){
$ Str = trim ($ str); // clear spaces on both sides of the string
$ Str = strip_tags ($ str, "<p>"); // clear the html format using the function provided by php. Keep P tag
$ Str = preg_replace ("/t/", "", $ str); // use a regular expression to match the content to be replaced, such as space, line feed, and will be replaced with null.
$ Str = preg_replace ("/rn/", "", $ str );
$ Str = preg_replace ("/r/", "", $ str );
$ Str = preg_replace ("/n/", "", $ str );
$ Str = preg_replace ("//", "", $ str );
$ Str = preg_replace ("//", "", $ str); // matches spaces in html
Return trim ($ str); // return a 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.