php过滤网站敏感关键词例子
在php中过滤敏感词的方法超级的简单我们只要使用strtr或者str_replace函数就可以直接快速的替换掉了,下面来两个简单好用的例子.
例子1,代码如下:
$badword = array ( '张三' , '丰田' ); $badword1 = array_combine ( $badword , array_fill (0, count ( $badword ), '*' )); $bb = '我今天开着上班' ; $str = strtr ( $bb , $badword1 );例子2,代码如下:
<?php function cleanWords1( $text ) { //根据个人需要添加需要过滤的词汇,以"|"作为分隔符 $badword = "敏感字|敏感字|敏感字|敏感字|敏感字" ; $badwords = explode ( '|' , $badword ); foreach ( $badwords as $v ){ $text = str_replace ( $v , '**' , $text ); } return $text ; } //开源代码phpfensi测试数据 function cleanWords2( $text ) { //根据个人需要添加需要过滤的词汇,以"|"作为分隔符 $badword = "敏感字|敏感字|敏感字|敏感字|敏感字" ; return preg_replace( "/$badword/i" , '**' , $text ); } $string = "敏感字符串" ; echo cleanWords1( $string ). '<br/>' ; echo cleanWords2( $string ); ?>查看更多关于php过滤网站敏感关键词例子 - php函数的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did30837