Function code for php to filter html tags

Source: Internet
Author: User
Php function code for filtering html tags this article provides four function codes for filtering html tags. Method 1 uses the strip_tags function provided by 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, and checks whether the tags are letters based on the ascii encoding value.

The most direct html filter method, the code is as follows:

Strip_tags ();

Method 2: Use regular expression filtering. the code is as follows:

Function _ filter ($ string ){

Return str_replace (array ("", ''), array ('
','
','
', ''), Strip_tags ($ string ,'

'));

}

The code is as follows: preg_replace ('/(
) {1,}/is ','
', $ Str );

The code is as follows:

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.phpfensi.com

';

$ Reg = '/( | ) | <. +?> /I ';

Echo preg_replace ($ reg, '$ 1', $ 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.