后端开发|php教程
php
后端开发-php教程
$str=”woxiangyao_genghao-de-zifuchuan”;
$arr = array(‘geng’,’de’,’cdaefad’);
foreach($arr as $v){
json 源码解析视频,修改vscode保护色,ubuntu主题+教程,tomcat如何启动cmd,python页面爬虫,php 输出系统时间,海口关键词seo排名,源码开发一个信息分类网站,宠物网页模板lzw
if(strpos($str, $v) !== false){ echo $str; exit;}
}
有没有效率更高的写法?
统计下跌源码,VScode怎么无法打开,ubuntu邮箱设置,tomcat-apach,sqlite执行完没有了,安卓手机爬虫信息下载软件,php大数据导出,seo推广站内,网站源码过360,网页表格边框代码,404页面 优秀模板下载lzw
b2cphp电商网站源码下载,苹果手机虚拟ubuntu,爬虫灰色收入,php constp,云建站seolzw
回复内容:
$str=”woxiangyao_genghao-de-zifuchuan”;
$arr = array(‘geng’,’de’,’cdaefad’);
foreach($arr as $v){
if(strpos($str, $v) !== false){ echo $str; exit;}
}
有没有效率更高的写法?
我将问题理解成如何判断内容是否包含敏感词,题主可将敏感词生成成字典树
,然后再查找内容是否包含关键词
下面是一个简单的PHP字典树
的示例,供参考
class TrieTree{ public $tree = array(); /*** 增加关键词到字典树** @param string $utf8_str */ public function add($utf8_str) { $chars = &UTF8Util::getChars($utf8_str); // 串结尾字符 $chars[] = null; $count = count($chars); $T = &$this->tree; for ($i = 0; $i _find($chars)) { $chars[] = null; $count = count($chars); $T = &$this->tree; for ($i = 0; $i _find($chars); } private function _find(&$chars) { $count = count($chars); $T = &$this->tree; for ($i = 0; $i tree; $count = 0; for ($i = 0; $i < $len; $i ++) { $c = $chars[$i]; // 起始字符匹配 if (array_key_exists($c, $Tree)) {$T = &$Tree[$c];for ($j = $i + 1; $j contain($str)) { return true;} } } return false; } /*** 导出序列化后的字典树** @return string*/ public function export() { return serialize($this->tree); } /*** 导入序列化后的字典树** @param string $str */ public function import($str) { $this->tree = unserialize($str); }}class UTF8Util{ public static function getChars($utf8_str) { $s = $utf8_str; $len = strlen($s); if ($len == 0) return array(); $chars = array(); for ($i = 0; $i > 7) == 0) {$chars[] = $c; } else // 1111 xxxx, first in four charif (($n >> 4) == 15) { if ($i > 5) == 7) { if ($i > 6) == 3) { if ($i < $len - 1) {$chars[] = $c . $s[$i + 1];$i ++; } } } return $chars; }}
如果不考虑语言角度,应该用AC自动机来做比较快