Example of specifying html tags for php regular expression filtering

Source: Internet
Author: User
Tags html tags mixed php regular expression regular expression

1. Specify tags for regular expression filtering

The code is as follows: Copy code

/**

* @ Param $ content
* @ Return mixed
* Filter the reserved content of Tag.
*/
Public function delete_tags_a ($ content ){
$ Content = preg_replace ("# <a [^>] *> (.*?) </A> # is "," $1 ", $ content );
Return $ content;
}

2. Filter all html tag content

The code is as follows: Copy code

/**
* @ Param $ content
* @ Param string $ tag: input the tag to be deleted.
* @ Param bool $ ident true: the tag content is retained; false: the tag content is not retained.
* @ Return mixed
*/
Public function delete_tags ($ content, $ tag = 'a', $ ident = true ){
If ($ ident ){
$ Content = preg_replace ("# <{$ tag} [^>] *> (.*?) </{$ Tag }>#is "," $1 ", $ content );
} Else {
$ Content = preg_replace ("/(<$ tag. *?> [\ S \ S] *? <\/$ Tag>)/",'', $ content );
}
Return $ content;
}

 

3.

The code is as follows: Copy code

/**
* @ Param $ str
* @ Return mixed | string
* Filter all html tags
*/
Public function deletehtml ($ str)
{
$ Str = trim ($ str );
$ Str = strip_tags ($ str ,"");
$ Str = preg_replace ("{\ t}", "", $ str );
$ Str = preg_replace ("{\ r \ n}", "", $ str );
$ Str = preg_replace ("{\ r}", "", $ str );
$ Str = preg_replace ("{\ n}", "", $ str );
$ Str = preg_replace ("{}", "", $ str );
$ Str = preg_replace ("{}", "", $ str );
Return $ str;
}

Of course, there are some php functions that can be directly filtered. They will filter all html tags except strings. I will not introduce this function.

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.