已有6000条关键字,分3批次。
一批为替换 replace,一批为遇到需要审核 censor,最后一批为遇到就禁止发布banned。
设计数据表如下:
mysql> desc tbl_censor;
+-------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------+----------------+
| id | smallint(6) unsigned | NO | PRI | NULL | auto_increment |
| censortype | smallint(6) | NO | | 1 | |
| find | varchar(120) | NO | UNI | | |
| replacement | varchar(255) | NO | | | |
| extra | varchar(255) | NO | | | |
| uptime | int(11) | YES | | NULL | |
| enable | int(1) | NO | | 1 | |
+-------------+----------------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)
由于有6000多关键字,使用 foreach 的 strstr?还是preg_match ?
追求效率,每小时提交量为10万多文章。
刚刚写的一种:
phpnamespace app\helpers;
use app\models\other\Censor;
use app\models\other\CensorLog;
class CensorHelper
{
public $id;
public $data;
public $match_banned;
public $match_censor;
public function __construct($id = 'censor')
{
$this->id = $id;
$this->match_banned = [];
$this->match_censor = [];
$this->data = $this->getData();
}
/**
* @description 获取正则表达式
* @return array|mixed
*/
public function getData()
{
$data = \Yii::$app->cache->get($this->id);
if (empty($data)) {
$words = Censor::find()
->where(['enable' => 1])
->andWhere([' != ', 'replacement', ''])
->orderBy(['replacement' => SORT_ASC, 'find' => SORT_DESC])
->asArray()
->all();
$censor = [];
$banned = [];
$replace = [];
foreach ($words as $row) {
switch ($row['replacement']) {
case '{censor}':
$censor[] = $row['find'];
break;
case '{banned}&#