当前位置: 首页 > 图文教程 > 网络编程 > PHP > 如何过滤高亮显示非法字符

PHP
MYSQL版本大于4.1问题 - PHPchina
怎么让用户点击一个连接后,把一个图片另存了 - PHPchina
武汉10月15日Phper聚会召集!!! - PHPchina
php如果不等待exec执行的程序创建的子进程? - PHPchina
哪位知道DISCUZ处理防SQL注入的代码是哪部分 - PHPchina
求教!我实在不知道哪里问题,在线等ing - PHPchina
怎样结束用户某一进程 - PHPchina
比对用户名密码能不能这样写? - PHPchina
求助:如何在PHP+mysql中实现数据备份? - PHPchina
大家看看这个配置对吗 - PHPchina
如何禁止require当前文件 - PHPchina
无法将回调函数放在类中? - PHPchina
村里 PHP代码高亮是怎么实现的? - PHPchina
apache安装后.服务里没有apache2这个服务! - PHPchina
请教一个小问题 - PHPchina
config.php里面是不是应该把多数参数设置为常量而不是变量? - PHPchina
请教高手一个问题 - PHPchina
如何让百度收录我的网站 ?? - PHPchina
谁能给个注入的简单语句? - PHPchina
求PHP站内搜索思路 - PHPchina

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 70 ::
收藏到网摘: n/a

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