PHP remove a specified HTML tag _ PHP Tutorial

Source: Internet
Author: User
PHP removes the specified HTML tag. 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 you to this function. in addition to this function, we can directly use the strip_tags function to replace the most commonly used specified HTML tag in php. you can use this function 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:


Strip_tags ($ source, "); remove the html tag.
Strip_tags ($ source ,'

'); Keep the p, 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:

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 = '(. + | S [^>] *>) | )';
}

$ Str = preg_replace ('# | S [^>] *>) '. $ content.' # is ', '', $ str );
}

Return $ str;
}


Parameter description


$ Str-indicates a string to be filtered, such as html tags such as p, p, em, and img.
$ 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:


$ Target = strip_only_tags ($ source, array ('A', 'em ',' B '); removes tags a, em, and B from the $ source string.

$ Source ='

This a example fromLixiphp!


';

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

// Target results
//

This a example from!

Other methods

The code is as follows:


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


A custom function

/

The code is as follows:
**
* 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 ,"

"); // Clear the html format using the built-in functions of 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.