1. Go to this page and download the library: Download jundayw/laravel-sensitive library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jundayw / laravel-sensitive example snippets
namespace App\Cloud;
use Illuminate\Support\Collection;
use Jundayw\LaravelSensitive\Contracts\InterceptorInterface;
class CloudInterceptor implements InterceptorInterface
{
public function handle(string $content, string $field): Collection
{
return collect([
// [
// 'stop_type' => 'REPLACE',
// 'stop_words' => '小额贷款',
// 'replacement' => 'xxx',
// ],
]);
}
}
Sensitive::username(string $content);
Sensitive::nickname(string $content);
Sensitive::message(string $content);
Sensitive::content(string $content);
Sensitive::filter(string $content, string $field = 'username|nickname|message|content', int $scope = SensitiveInterface::STATUS_ALL);
use Jundayw\LaravelSensitive\Sensitive;
public function handle(SensitiveInterface $sensitive): void
{
$content = '本校小额贷款,安全、快捷、方便、无抵押,随机随贷,当天放款,上门服务。';
$instance = $sensitive->content($content);
if ($instance->isBlock()) {
// 含有黑名单词语
}
if ($instance->isReview()) {
// 含有待审核词语
}
if ($instance->isReplace()) {
// 含有敏感词词语已被替换
$content = $instance->getContent();
}
var_dump($content);
}
use Jundayw\LaravelSensitive\Facades\Sensitive;
try {
$content = '本校小额贷款,安全、快捷、方便、无抵押,随机随贷,当天放款,上门服务。';
$instance = Sensitive::listen(SensitiveInterface::STATUS_REVIEW | SensitiveInterface::STATUS_BLOCK, function () {
throw new Exception('含有敏感词');
})->content($content);
var_dump($instance->getContent());
} catch (Exception $exception) {
// $exception->getMessage();
}