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 );