Php removes HTML Tag instances

Source: Internet
Author: User
In php, there are many HTML Tag methods to remove strings. The most common method is to use the strip_tags function together to keep only characters, the regular expression is also used in addition to selective removal. below

In php, there are many HTML Tag methods to remove strings. The most common method is to use the strip_tags function together to keep only characters, the regular expression is also used in addition to selective removal. below

Example 1: php built-in functions can remove/delete HTML tags/codes from strings.
Strip_tags (string, allow): The function removes HTML, XML, and PHP labels.
Parameter: string, required. It specifies the string to be checked; allow, optional. It specifies the tags that are allowed and will not be deleted.

The Code is as follows:


$ Str = 'Guo bowl basin-php ';
$ Str1 = strip_tags ($ str); // delete all HTML tags
$ Str2 = strip_tags ($ str, ''); // keep tags
Echo $ str1; // output Guo bowl basin-PHP
Echo $ str2; // different styles

Example 2: Clear some attribute codes in the HTML Tag string
Use PHP to process the HTML code of the document read from the database, and then use regular expressions to match and modify the code. Save the code and use the static generation function to generate the modified HTML page.
Because the generated code is already processed, the loading speed of HTML pages will not be affected.
The [Sample Code] is as follows:

The Code is as follows:


$ Str = "";
$ Str = preg_replace ('# onclick = ([sS] *) "#','', $ str );
// # Note that there is a double quotation mark before the symbol, indicating the end code
Print ($ str );
?>

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.