PHP的一个过滤敏感词或脏话的方法
主要使用了 int substr_count ( string haystack, string needle [, int offset [, int length]] ) 这个方法,这个方法遍历待测的字符串$str中有没有$allergicWord数组中所包含的敏感词:
$allergicWord = array('脏话','骂人话');
$str = '这句话里包含了脏话和骂人话';
for ($i=0;$i<count($allergicWord);$i++){
$content = substr_count($str, $allergicWord[$i]);
if($content>0){
$info = $content;
break;
}
}
if($info>0){
//有违法字符
return TRUE;
}else{
//没有违法字符
return FALSE;
}
如果需要将出现的敏感词替换,比如替换###或者***可以结合substr_replace ( mixed string, string replacement, int start [, int length] )方法使用
相关文档:
$_SERVER['PHP_SELF'] 函数用法 #当前正在执行脚本的文件名,与 document root相关。
$_SERVER['argv'] 函数用法 #传递给该脚本的参数。
$_SERVER['argc'] 函数用法 #包含传递给程序的命令行参数的个数(如果运行在命令行模式)。
$_SERVER['GATEWAY_INTERFACE'] 函数用法 #服务器使用的 CGI 规范的版本。例如,&ldqu ......
一:结构和调用(实例化):
class className{} ,调用:$obj = new className();当类有构造函数时,还应传入参数。如$obj = new className($v,$v2...);
二:构造函数和析构函数:
1、构造函数用于初始化:使用__construct(),可带参数。
2、但析构函数不能带参数(用于在销去一个类之前执行一些操作或功能)。析构函数用 ......
抓取到的内容在通过正则表达式做一下过滤就得到了你想要的内容,至于如何用正则表达式过滤,在这里就不做介绍了,有兴趣的,以下就是几种常用的用php抓取网页中的内容的方法。
1.file_get_contents
PHP代码
复制代码 代码如下:
<?php
$url = "http://www.jb51.net";
$contents = file_get_contents($url);
// ......
刚换了一个工作,现在没什么事做,写了一个数据缓存的类。
可以缓存数组,字符,对象等,执行效率还没有测试,先放出来吧。
实例如下:
* @example
* require 'MyCache.class.php';
* $mc = new MyCache("./test/cache");
* $a = "hello world111";
* $mc->set("ss", $a);
......
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;
$root = $doc->createElement('document'); //创建根
$doc->appendChild($root); //加入根
//webSite
$webSite = $doc->createElement('webSite');
$webSite->appendchild($doc ......