Home > php教程 > php手册 > 给whygod,如何过滤高亮显示非法字符!

给whygod,如何过滤高亮显示非法字符!

WBOY
Release: 2016-06-21 09:12:32
Original
982 people have browsed it

高亮显示


//我只是做一个测试,如果再用类似tmd的字符串,一定要注意,如果字符串中的字符出现在$StartReplaceHtml或者$EndReplaceHtml中,你需要修改下面的preg_replace中的规则
$CheckedMessage="Hello!!他X的..他....X...的....How are you m d ";     //the message to be checked
echo $CheckedMessage."
";
$CheckedWords=array("他X的","tmd");//要过滤的非法字符,如果你需要把单个的字符也过滤,比如:他ererX000的过滤出来他,X,的,你就没必要把过滤字符设置成"他X的"只要设置成$CheckedWords=array("他","X","的");就可以了
$StartReplaceHtml="";
$EndReplaceHtml="
";
$CheckOther= true;//设置标示位,是否显示单个的字符,如果设置成true,下面的if(strstr……)就不需要了
for($i=0;$i     if(strstr($CheckedMessage,$CheckedWords[$i])){ //此处可去掉
         $CheckedMessage=eregi_replace($CheckedWords[$i],$StartReplaceHtml.$CheckedWords[$i].$EndReplaceHtml,$CheckedMessage);//如果只过滤诸如"他X的"字符串(是字符串,不是单个字符),可以直接写这句,同时把$CheckOther设置成false
     }
     if($CheckOther == true){
         $CharStringLength = strlen($CheckedWords[$i]);
         for($j=0;$j             $AssumeLength=1;//假定截取长度
             if(ord(substr($CheckedWords[$i],$j,$AssumeLength))>0xa0){//如果汉字,假定长度加一
                $AssumeLength++;         
             }
             $SubstrChar = substr($CheckedWords[$i],$j,$AssumeLength);             
             $CheckedMessage=preg_replace("/(?中的t不会被替换,如果需要过滤掉在$StartReplace或者在$EndReplaceHtml中的的字符,需要修改规则,否则的话将会出现乱码             
             if($AssumeLength!=1){//如果当前截取字符为汉字
             $j++;        
             }
         
         }
     }
}
echo $CheckedMessage;
//只是一个简单的测试,希望对你有所帮助,希望你能举一反三,好运!

?>



Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template