Php function code for filtering html tags _ PHP Tutorial

Source: Internet
Author: User
Function code used by php to filter html tags. Php function code for filtering html tags this article provides four function codes for filtering html tags using php, method 1 uses the strip_tags function provided by php to filter all html tags. function code used by php to filter html tags. This article provides four function codes used by php to filter html tags, method 1 uses the strip_tags function of php to filter all html tags. Method 2 uses regular expressions to filter html tags, the third method is to clear the user-defined function of the HTML tag, and determine whether it is a letter based on the ascii code value before filtering.

Function code for filtering html tags in the php Tutorial
This article provides four function codes that use php to filter html tags. Method 1 uses the strip_tags function of php to filter all html tags, method 2 uses regular expressions to filter html tags. Method 3 is the user-defined function used to clear html tags. the user-defined function checks whether the tags are letters based on the ascii encoding value before filtering.
*/
// The most direct html filtering method

Strip_tags ();

// Method 2 use regular expression filtering
Function _ filter ($ string ){
Return str_replace (array ("n", "rn", "r", ''), array ('
','
','
', ''), Strip_tags ($ string ,'

'));
}

// Regular expression 2

Preg_replace ('/(
) {1,}/is ','
', $ Str );


// Regular expression 3

Function delhtml ($ str) {// clear html tags
$ St =-1; // start
$ Et =-1; // end
$ Stmp = array ();
$ Stmp [] = "";
$ Len = strlen ($ str );
For ($ I = 0; $ I <$ len; $ I ++ ){
$ Ss = substr ($ str, $ I, 1 );
If (ord ($ ss) = 60) {// ord ("<") = 60
$ St = $ I;
}
If (ord ($ ss) = 62) {// ord (">") = 62
$ Et = $ I;
If ($ st! =-1 ){
$ Stmp [] = substr ($ str, $ st, $ et-$ st + 1 );
}
}
}
$ Str = str_replace ($ stmp, "", $ str );
Return $ str;
}
//

$ Str ='


Www.bkjia.com />

';
$ Reg = '/( | ) | <. +?> /I ';
Echo preg_replace ($ reg, '$ 1', $ str );


Http://www.bkjia.com/PHPjc/445430.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445430.htmlTechArticlephp filter html tag function code this article provides four use php filter html tag function code, method 1 uses the strip_tags function of php to filter all html tags...

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.