1. composer require lustre/php-dfa-sensitive
2. routes/web.php
$router->get('/test-sensitive', 'SensitiveController@test');
3. 新建 app\Http\Controller\SensitiveController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers;
use DfaFilter\Exceptions\PdsBusinessException;
use DfaFilter\Exceptions\PdsSystemException;
use DfaFilter\SensitiveHelper;
use Exception;
use Illuminate\Http\Request;
class SensitiveController extends Controller
{public function test(Request $request)
{$wordFilePath = dirname(dirname(__DIR__)) . '/Sensitive/tests/data/words.txt';
try {$handle = SensitiveHelper::init()->setTreeByFile($wordFilePath);
} catch (PdsBusinessException $e) {throw new Exception(" 没有取到敏感词库 ");
}
// dd($handle);
$data = $request->input();
if ($islegal = $handle->islegal($data['name'])) {// return response($data['name'] . ' 为敏感词 ');
$inputData = $handle->replace($data['name'], '*', true);
}
dd($inputData);
}
}
正文完