search
Homephp教程php手册如何过滤高亮显示非法字符

如何过滤高亮显示非法字符

Jun 13, 2016 pm 12:43 PM
oneDohowcharacterstringIshowNoticetestoffilterHighlight


//我只是做一个测试,如果再用类似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;
//只是一个简单的测试,希望对你有所帮助,希望你能举一反三,好运!

?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft