过滤掉 Laravel 集合中包含特定单词的评论

问题描述 投票:0回答:3

我正在尝试过滤掉其中包含特定单词的评论。我正在显示随机的积极评论。我不希望它们包含“太”、“小”、“请”、“下一个”等词。评论最少 3 个字。

每条笔记和评论评分都是一个数据库字段

我有从 $commentsGood 数组中获取随机评论的代码。我想确保它们不包含上面提到的那些词。另外,如果有人知道如何简化我当前的数组,我很想学习新的东西,谢谢。

编辑以显示数据被抓取以及返回的内容。

$commentsRatings = Rating::all();

                $commentsGood = collect([
                $comments1 = $commentsRatings->where('commentsRating_1', '=', 5)->where('notes_1', '!=', '')->pluck('notes_1', 'feedback_user_id')->all(),
                $comments2 = $commentsRatings->where('commentsRating_2', '=', 5)->where('notes_1', '!=', '')->pluck('notes_2', 'feedback_user_id')->all(),
                $comments3 = $commentsRatings->where('commentsRating_3', '=', 5)->where('notes_1', '!=', '')->pluck('notes_3', 'feedback_user_id')->all(),
                $comments3 = $commentsRatings->where('commentsRating_4', '=', 5)->where('notes_1', '!=', '')->pluck('notes_4', 'feedback_user_id')->all(),
                $comments5 = $commentsRatings->where('commentsRating_5', '=', 5)->where('notes_1', '!=', '')->pluck('notes_5', 'feedback_user_id')->all(),
                $comments6 = $commentsRatings->where('commentsRating_6', '=', 5)->where('notes_1', '!=', '')->pluck('notes_6', 'feedback_user_id')->all(),
                $comments7 = $commentsRatings->where('commentsRating_7', '=', 5)->where('notes_1', '!=', '')->pluck('notes_7', 'feedback_user_id')->all(),
                $comments8 = $commentsRatings->where('commentsRating_8', '=', 5)->where('notes_1', '!=', '')->pluck('notes_8', 'feedback_user_id')->all()
              ]);

dd($commentsGood);

退货

Collection {#332 ▼
   #items: array:8 [▼
0 => array:279 [▶]
1 => array:205 [▶]
2 => array:194 [▶]
3 => array:115 [▶]
4 => array:46 [▶]
5 => array:29 [▶]
6 => array:13 [▶]
7 => array:4 [▼
  0 => "Can you please provide some answers"
  1 => "Perfect!"
  2 => "This is just too much to read"
  3 => "a little tight in the shoulders  "
]]}

在这个数组数组中,我想删除所有包含上述单词的完整注释。

然后我运行 shuffle->take(1)->first 得到类似的东西。仅获取其中一个数组,然后从那里获取随机行。 例如,数组返回句子而不是单个单词

array:29 [▼
  0 => "I typically would not have worn something like this but just 
  loved it. "
  1 => "please take this comment"
  2 => "Cool jeans, but one size too large."
php laravel collections filtering
3个回答
1
投票

我有从

$commentsGood
数组中获取随机注释的代码。我想确保它们不包含上面提到的那些词。

由于您已经从该集合中随机获取字符串并且您只想检查它,因此请使用 Laravel 的

str_contains()
(不是 PHP 原生的
str_contains()
)以及数组作为第二个参数:

if (str_contains($text, ['too', 'small', 'please', 'next']))

0
投票

也许这会对你有帮助。

如果您有 $commentsGood 作为所需单词的数组,那么(只需更新此方法以使用您的列和模型):

$disabledWords = ['too', 'small', 'please', 'next'];

$comments = Model::where(function ($q) use ($disabledWords) {
    foreach ($disabledWords as $value) {
        $q->orWhere('comment', 'not like', "%{$value}%");
    }
})->get();

您可以尝试

like
not like


以你的例子:

$disabledWords = ['too', 'small', 'please', 'next'];

$comments1 = $commentsRatings->where('commentsRating_1', '=', 5)
    ->where('notes_1', '!=', '')
    ->pluck('notes_1', 'feedback_user_id')
    ->where(function ($q) use ($disabledWords) {
        foreach ($disabledWords as $value) {
            $q->orWhere('comment', 'not like', "%{$value}%");
        }
    })
    ->get();

请创建一个更简单、更干净的函数;) 例如:

public function getComments($commentsType, $notesType)
{
    $disabledWords = ['too', 'small', 'please', 'next'];
    return $this
        ->where($commentsType, '=', 5)
        ->where('notes_1', '!=', '')
        ->pluck($notesType, 'feedback_user_id')
        ->where(function ($q) use ($disabledWords) {
            foreach ($disabledWords as $value) {
                $q->orWhere('comment', 'not like', "%{$value}%");
            }
        })
        ->get();
}

然后使用:

$commentsGood = collect([
    $commentsRatings->getComments('commentsRating_1', 'notes_1'),
    $commentsRatings->getComments('commentsRating_2', 'notes_2'),
    $commentsRatings->getComments('commentsRating_3', 'notes_3'),
    $commentsRatings->getComments('commentsRating_4', 'notes_4'),
    $commentsRatings->getComments('commentsRating_5', 'notes_5'),
    $commentsRatings->getComments('commentsRating_6', 'notes_6'),
    $commentsRatings->getComments('commentsRating_7', 'notes_7'),
    $commentsRatings->getComments('commentsRating_8', 'notes_8'),
]);

0
投票

使用

map()
集合方法来映射你的集合,
array_filter()
来过滤你的数组,并使用 Laravel 的
str_contains()
(不是 PHP 的
str_contains()
)来检查:

$commentsGood = $commentsGood->map(function($item){
        return array_filter($item, function($i){
            return !str_contains($i, ['too', 'small', 'please', 'next']);
        });
})->values();

然后随机选择

one

© www.soinside.com 2019 - 2024. All rights reserved.