我正在尝试过滤掉其中包含特定字词的评论。我正在展示随机的积极评论。我不希望它们包含类似的词,小,请,接下来。而且评论至少有3个单词。
每个notes和commentsRating都是一个数据库字段
我有代码从$ 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) - >首先得到类似的东西。只得到一个数组然后从那里得到一个随机线。 数组返回的句子不是单个单词,例如
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."
答案 0 :(得分:1)
我有代码从
$commentsGood
数组中抓取一个随机注释。我想确保他们不包含上述那些词。
由于您已经从此集合中随机获取字符串而您只想检查它,请使用带有数组的str_containts
作为第二个参数:
if (str_contains($text, ['too', 'small', 'please', 'next']))
答案 1 :(得分: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'),
]);
答案 2 :(得分:0)
使用map()
收集方法来映射您的收藏集,array_filter()
来过滤您的数组,str_contains
来检查:
$commentsGood = $commentsGood->map(function($item){
return array_filter($item, function($i){
return !str_contains($i, ['too', 'small', 'please', 'next']);
});
})->values();
然后随机选择one
。