本文目录导读:

在 PHP 中实现文本鉴黄,通常有以下几种方案,按推荐程度排序:
使用第三方云服务(推荐)
这是最准确、维护成本最低的方案。
安全
<?php
require 'vendor/autoload.php';
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
AlibabaCloud::accessKeyClient('your-access-key', 'your-access-secret')
->regionId('cn-shanghai')
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('Green')
->version('2018-05-09')
->action('TextScan')
->method('POST')
->options([
'query' => [
'body' => json_encode([
'tasks' => [
[
'dataId' => 'your-data-id',
'content' => '待检测的文本内容'
]
],
'scenes' => ['antispam'] // 审核场景
])
]
])
->request();
$data = $result->toArray();
if ($data['code'] == 200) {
$taskResult = $data['data'][0];
if ($taskResult['code'] == 200) {
foreach ($taskResult['results'] as $result) {
echo "命中标签: " . $result['label'] . "\n";
echo "置信度: " . $result['confidence'] . "\n";
echo "建议: " . $result['suggestion'] . "\n";
// 处理结果
if ($result['suggestion'] === 'block') {
// 拒绝内容
} elseif ($result['suggestion'] === 'review') {
// 人工审核
}
}
}
}
} catch (ClientException $e) {
echo $e->getErrorMessage();
} catch (ServerException $e) {
echo $e->getErrorMessage();
}
?>
安全
<?php
require 'vendor/autoload.php';
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Tms\V20201229\TmsClient;
use TencentCloud\Tms\V20201229\Models\TextModerationRequest;
$cred = new Credential("your-secret-id", "your-secret-key");
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("tms.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new TmsClient($cred, "ap-guangzhou", $clientProfile);
$req = new TextModerationRequest();
$req->setContent("待检测的文本内容");
try {
$resp = $client->TextModeration($req);
// 获取检测结果
echo $resp->getSuggestion(); // Pass, Review, Block
echo $resp->getLabel(); // 内容标签
var_dump($resp->getDetailResults());
} catch (TencentCloudSDKException $e) {
echo $e;
}
?>
使用开源敏感词库(本地方案)
Composer 安装
composer require lestonn/ban-word
<?php
use Lestonn\BanWord;
$banWord = new BanWord([
'file' => 'path/to/word-list.txt', // 敏感词库文件
]);
// 检测是否存在敏感词
if ($banWord->has('需要检测的文本')) {
echo '包含敏感词';
}
// 过滤敏感词
$cleanText = $banWord->filter('需要过滤的文本');
echo $cleanText;
// 获取命中的敏感词
$badWords = $banWord->check('需要检测的文本');
print_r($badWords);
?>
自定义敏感词检测
<?php
class SensitiveWordFilter
{
private $dict = [];
private $minLen = 2;
public function __construct($dictPath = null)
{
if ($dictPath) {
$this->loadDict($dictPath);
}
}
// 加载敏感词库
public function loadDict($path)
{
$words = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($words as $word) {
$this->dict[] = trim($word);
}
}
// 添加敏感词
public function addWord($word)
{
$this->dict[] = $word;
}
// 检测是否包含敏感词
public function isSensitive($text)
{
foreach ($this->dict as $word) {
if (stripos($text, $word) !== false) {
return true;
}
}
return false;
}
// 替换敏感词
public function filter($text, $replaceChar = '*')
{
foreach ($this->dict as $word) {
$replacement = str_repeat($replaceChar, mb_strlen($word));
$text = str_ireplace($word, $replacement, $text);
}
return $text;
}
// 检测所有命中的敏感词
public function check($text)
{
$hits = [];
foreach ($this->dict as $word) {
if (stripos($text, $word) !== false) {
$hits[] = $word;
}
}
return $hits;
}
}
// 使用示例
$filter = new SensitiveWordFilter('sensitive_words.txt');
$text = '这是一段需要检测的文本';
if ($filter->isSensitive($text)) {
echo '发现敏感词';
print_r($filter->check($text));
// 过滤敏感词
$cleanText = $filter->filter($text);
echo $cleanText;
}
?>
机器学习方案(高级)
使用 PHP-ML 进行文本分类
<?php
require 'vendor/autoload.php';
use Phpml\Classification\NaiveBayes;
use Phpml\FeatureExtraction\TokenCountVectorizer;
use Phpml\Tokenization\WhitespaceTokenizer;
use Phpml\FeatureExtraction\TfIdfTransformer;
// 训练数据
$samples = [
['这是正常的内容'],
['这是健康的学习资料'],
['这些成人内容需要检测'],
['含有不当词语的内容'],
['正常的购物信息'],
];
$labels = [
'normal',
'normal',
'adult',
'adult',
'normal',
];
// 特征提取
$vectorizer = new TokenCountVectorizer(new WhitespaceTokenizer());
$vectorizer->fit($samples);
$vectorizer->transform($samples);
$transformer = new TfIdfTransformer();
$transformer->fit($samples);
$transformer->transform($samples);
// 训练模型
$classifier = new NaiveBayes();
$classifier->train($samples, $labels);
// 预测新文本
$newText = ['这段新文本需要检测'];
$vectorizer->transform($newText);
$transformer->transform($newText);
$prediction = $classifier->predict($newText[0]);
echo '预测结果: ' . $prediction;
?>
完整方案示例
一个完整的生产级方案:
<?php
class ContentModeration {
private $config;
public function __construct($config) {
$this->config = $config;
}
public function moderateText($text) {
// 1. 本地快速检测(成本低)
$localCheck = $this->localCheck($text);
if ($localCheck['isSensitive']) {
return [
'status' => 'block',
'result' => $localCheck
];
}
// 2. 云服务详细检测(准确)
$cloudCheck = $this->cloudCheck($text);
// 3. 综合判断
if ($cloudCheck['code'] == 200) {
switch ($cloudCheck['suggestion']) {
case 'block':
return ['status' => 'block', 'result' => $cloudCheck];
case 'review':
return ['status' => 'review', 'result' => $cloudCheck];
default:
return ['status' => 'pass', 'result' => $cloudCheck];
}
}
// 4. 云服务失败时的降级策略
return ['status' => 'review', 'error' => '检测服务异常'];
}
private function localCheck($text) {
// 实现本地敏感词检测
$sensitiveWords = ['敏感词1', '敏感词2'];
$hits = [];
foreach ($sensitiveWords as $word) {
if (mb_strpos($text, $word) !== false) {
$hits[] = $word;
}
}
return [
'isSensitive' => count($hits) > 0,
'hits' => $hits
];
}
private function cloudCheck($text) {
// 调用云服务API
// 这里以阿里云为例
try {
// 调用阿里云API
$result = $this->callAliyunAPI($text);
return $result;
} catch (\Exception $e) {
// 记录日志
error_log('Cloud check failed: ' . $e->getMessage());
return ['code' => 500];
}
}
}
// 使用示例
$config = [
'aliyun' => [
'access_key' => 'your-key',
'access_secret' => 'your-secret',
]
];
$moderator = new ContentModeration($config);
$result = $moderator->moderateText('需要检测的文本');
if ($result['status'] === 'block') {
echo '内容被拒绝';
} elseif ($result['status'] === 'review') {
echo '需要人工审核';
} else {
echo '内容正常';
}
?>
性能优化建议
<?php
// 1. 缓存结果
$cache = new Redis();
$cacheKey = 'moderation:' . md5($text);
if ($cached = $cache->get($cacheKey)) {
return json_decode($cached, true);
}
// 2. 异步处理
// 使用队列进行异步审核
$q = new Queue();
$q->push('moderation', $text);
// 3. 批量检测
$batchTexts = [];
foreach ($texts as $text) {
$batchTexts[] = $text;
if (count($batchTexts) >= 10) {
$result = $moderator->moderateBatch($batchTexts);
$batchTexts = [];
}
}
?>
注意事项
- 合规性:确保使用符合当地法律法规的审核服务
- 性能优化:使用缓存、异步处理减少延迟
- 降级策略:云服务不可用时要有备用方案
- 日志记录:记录所有审核结果,便于审计
- 人工审核:对于不确定的内容要有人工审核机制
推荐使用成熟的云服务提供商,准确性高且维护成本低,如果对数据隐私有严格要求,可以选择本地部署方案。