Php code for clearing HTML tags in strings

Source: Internet
Author: User
To filter all html tags in a string, you can write a function by yourself and use regular expression filtering. The other method is to use the strip_tags function provided by php. This article describes how to clear html tags from strings in php.
To filter all html tags in a string, you can write a function by yourself and use regular expression filtering. The other method is to use the strip_tags function provided by php.

The code is as follows:


Function clear_html_label ($ html)
{
$ Search = array ("' ] *?>. *? Script 'Si "," '<[/!] *? [^ <>] *?> 'Si "," '([rn]) [s] +' "," '& (quot | #34);' I ", "'& (amp | #38);' I", "'& (lt | #60);' I", "'& (gt | #62 ); 'I ","' & (nbsp | #160); 'I ","' & (iexcl | #161); 'I ", "'& (cent | #162);' I", "'& (pound | #163);' I", "'& (copy | #169 ); 'I ","' & # (d +); 'e ");
$ Replace = array ("", "", "1", "," & "," <","> "," ", chr (161 ), chr (162), chr (163), chr (169), "chr (1 )");

Return preg_replace ($ search, $ replace, $ html );
}

// Instance application

$ String = 'AAA'
Script fdsafsa ';
Echo clear_html_label ($ string); // aaa fdsafsa

// Use the php built-in function strip_tags (); www.zzarea.com
Echo strip_tags ($ string); // aaa fdsafsa



Conclusion,
The results of the above two functions are exactly the same. one is the user-defined filtering of all html functions, and the other is the php built-in function, but in green, the strip_tags () function of php is used, it must be much higher. At least I won't say much.

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.