使用DFA算法,Laravel有已實現的庫:https://github.com/yankewei/laravel-sensitive
問題1:詞庫的加載需要在系統啓動時添加一次,而非每次需要過濾時都加載一遍
在SensitiveServiceProvider中的boot方法中加載即可。
在Laravel的Provider中,所有的register函數調用一遍後,再調用boot函數。
問題2:SensitiveServiceProvider中的boot方法中調用Sensitive門面方法會提示
Sensitive::addwords($filename);
ErrorException : Non-static method Yankewei\LaravelSensitive\Sensitive::addWords() should not be called statically
改爲如下方式即可:
app(Sensitive::class)->addwords($filename);