我试图过滤掉评论中有特定的词。我显示随机的积极评论。我不希望它们包含太,小,请,下一个这样的词。并且评论至少有3个词。
每个注解和评论Rating都是一个数据库字段
我有一个代码,它从$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."
型
3条答案
按热度按时间qmb5sa221#
我有一段代码,从
$commentsGood
数组中随机抓取一个注解。我想确保它们不包含上面提到的那些单词。由于你已经从这个集合中随机获取了字符串,并且你只想检查它,使用Laravel的
str_contains()
(不是PHP的原生str_contains()
)和一个数组作为第二个参数:字符串
2ekbmq322#
也许这个能帮到你
如果你有$commentsGood作为所需单词的数组,那么(只需更新此方法以使用您的列和模型):
字符串
您可以尝试
like
或not like
以你为例:
型
请创建一个更简单和干净的函数;)例如:
型
然后用途:
型
ruarlubt3#
使用
map()
集合方法来Map你的集合,array_filter()
来过滤你的数组,Laravel的str_contains()
(不是PHP的str_contains()
)来检查:字符串
然后随机选取
one
。