【问题标题】:Is there a way to filter bad words in Laravel using rule有没有办法使用规则过滤Laravel中的坏词
【发布时间】:2022-12-08 05:13:45
【问题描述】:

我正在寻找建立一个关键字过滤规则我已经成功运行php artisan make:rule NoInvalidKeywords

我的 NoInvalideKeywords.php 中有这个

public function passes($attribute, $value)
    {
         return ! collect(explode(' ', $value))->contains(function ($word){
            return Str::contains($word, ['video']);
        });
    }

上面的代码不检查是否区分大小写,例如,如果 Video 作为标题给出,则不检查规则。

【问题讨论】:

标签: php laravel


【解决方案1】:

您可以将 $word 转换为小写然后检查:

function passes($attribute, $value)
    {
         return ! collect(explode(' ', $value))->contains(function ($word){
            return Str::contains(strtolower($word), ['video']);
        });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多