PHP code example of sydante / laravel-sensitive

1. Go to this page and download the library: Download sydante/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/ */

    

sydante / laravel-sensitive example snippets


'providers' => [
    //...
    Sydante\LaravelSensitive\ServiceProvider::class,
],

'aliases' => [
    //...
    'Sensitive' => Sydante\LaravelSensitive\Facades\Sensitive::class,
],

// Laravel 可直接通过如下方式获取实例
$sensitive = app(Sydante\LaravelSensitive\Sensitive::class);

$sensitive->filter('这是一个示例');

// 或者使用 Facade
\Sydante\LaravelSensitive\Facades\Sensitive::filter('这是一个示例');


// 其他骚操作:

// 1、动态修改敏感词,并在使用缓存时缓存
$sensitive->emptyTrieTreeMap()
    ->addWords(['test'])
    ->saveTrieTreeMap();

// 2、重置敏感词为初始化时的默认设置,并在使用缓存时缓存
$sensitive->resetTrieTreeMap()
    ->saveTrieTreeMap();

use Sydante\LaravelSensitive\Sensitive;
use Sydante\LaravelSensitive\SensitiveCacheInterface;

// 编写自定义的缓存类
class CustomSensitiveCache implements SensitiveCacheInterface
{
    // ...
}

// 使用
$sensitive = new Sensitive([
    'cache' => true,
    'words' => ['test'],
    // 使用自定义缓存类
    'cache_class' => CustomSensitiveCache::class,
]);

$sensitive->filter('我们来一个test'); // 我们来一个****

// in config/sensitive.php

// 获取设置的干扰因子
$disturbs = env('SENSITIVE_DISTURBS');

if ($disturbs !== null) {
    // 将干扰因子解析成单个字符的列表
    $disturbs = array_values(array_unique(
        preg_split('//u', $disturbs)
    ));
}

return [
    // 是否使用缓存,默认 false
    'cache' => env('SENSITIVE_CACHE', false),

    // 干扰因子列表
    'disturbs' => $disturbs,

    // ...
];
bash
php artisan vendor:publish --provider="Sydante\LaravelSensitive\ServiceProvider"
bash
# 更新缓存
php artisan sensitive:cache
bash
php artisan sensitive:clear