love_php/app/Traits/ChatMessage.php

34 lines
1.3 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Traits;
trait ChatMessage
{
/**
* 过滤文字
* @param $content
* @return array|string|string[]
*/
public function filterContent($content)
{
$preg_wechat = '/[1-9]([0-9]{5,11})|0?(13|14|15|18)[0-9]{9}|[a-zA-Z]{6}|(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]{5}|[\x80-\xff]+[a-z-A-Z]{5}|((0|[1-9]\d*)(\.\d+)?){5}|(零|一|二|三|四|五|六|七|八|九|十)(百|十|零)?(一|二|三|四|五|六|七|八|九)?(百|十|零)?(一|二|三|四|五|六|七|八|九){2}|(①|②|③|④|⑤|⑥|⑦|⑧|⑨|⑩)?(①|②|③|④|⑤|⑥|⑦|⑧|⑨|⑩)?(①|②|③|④|⑤|⑥|⑦|⑧|⑨|⑩)|(壹|贰|叁|肆|伍|陆|柒|捌|玖|零|拾)|(❶|❷|❸|❹|❺|❻|❼|❽|❾|❿)+/';
$filter = (bool)preg_match($preg_wechat, preg_quote($content));
//已经违规
if ($filter) {
//替换正则数据
$content = preg_replace_callback($preg_wechat, function ($arr) {
if (!empty($arr[0])) {
$str = '';
for ($i = 0; $i < mb_strlen($arr[0]); $i++) {
$str .= '*';
}
$arr[0] = $str;
}
return $arr[0];
}, $content);
}
return str_replace('\\', '', $content);
}
}