Lumen7.x (使用笔记十四) 敏感词过滤

345 次浏览次阅读
没有评论

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);
    }
}
正文完
 0
评论(没有评论)