Home  >  Article  >  Backend Development  >  php function code for filtering html tags

php function code for filtering html tags

高洛峰
高洛峰Original
2016-11-29 15:01:071131browse

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 = '/(|)|<.+?>/i';

echo preg_replace($reg,'$1',$str);


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn