Home >Backend Development >PHP Tutorial >php function code for filtering html tags
This article provides four function codes for filtering html tags. The first method is the simplest to use PHP's own function strip_tags to filter all html tags. The second method uses regular expressions to filter html tags. The third method is to clear HTML tags. User-defined function, based on the ascii encoding value to determine whether it is a letter and then filter.
The most direct way to filter html, the code is as follows:
strip_tags();
Method 2 uses regular filtering, the code is as follows:
function _filter ( $string ) {
return str_replace(array(" "," "," ",' '),array('
','
','
',' '),strip_tags($string,'
'));
}
Regular 2, the code is as follows: preg_replace('/(< br>){1,}/is','
', $str);
Regular 3, the code is as follows:
function delhtml($str){ //Clear html tag
$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 = '/(?p>|
echo preg_replace($reg,'$1',$str);