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

iqih9akk  于 5个月前  发布在  PHP
关注(0)|答案(3)|浏览(35)

我试图过滤掉评论中有特定的词。我显示随机的积极评论。我不希望它们包含太,小,请,下一个这样的词。并且评论至少有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."

qmb5sa22

qmb5sa221#

我有一段代码,从$commentsGood数组中随机抓取一个注解。我想确保它们不包含上面提到的那些单词。
由于你已经从这个集合中随机获取了字符串,并且你只想检查它,使用Laravel的str_contains()(不是PHP的原生str_contains())和一个数组作为第二个参数:

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

字符串

2ekbmq32

2ekbmq322#

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

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

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

字符串
您可以尝试likenot 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'),
]);

ruarlubt

ruarlubt3#

使用map()集合方法来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

相关问题