Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将集合中的匹配字存储在db中_Php_Laravel - Fatal编程技术网

Php 将集合中的匹配字存储在db中

Php 将集合中的匹配字存储在db中,php,laravel,Php,Laravel,我有一个函数来匹配一个坏单词。我正在寻找匹配的词 Public function store(PostRequest $request){ $post = Post::create($request->validated()); $warningWords = Word::where('type', 'warning_words')->pluck('content')->toArray(); if($this->match($forbiddenP

我有一个函数来匹配一个坏单词。我正在寻找匹配的词

Public function store(PostRequest $request){ 
    $post = Post::create($request->validated());
    $warningWords = Word::where('type', 'warning_words')->pluck('content')->toArray();
    if($this->match($forbiddenPhrases, $thought_content)){
        Flagable::create('status' => '2', 'word' => '', 'word_id' => '']);
    }
}

private function match($needles, $haystack){
    foreach($needles as $needle){
        if (strpos($haystack, $needle) !== false) {
            return true;
        }
    }
    return false;
}

如何将匹配的单词存储在create语句列“word”中我认为最好的解决方案是让函数在找到单词时返回单词,如果没有找到,则返回null

private function match($needles, $haystack)
{
    foreach ($needles as $needle) {
        if (strpos($haystack, $needle) !== false) {
            return $needle;
        }
    }
}
现在,您可以使用if语句检查是否返回了单词:

$word = $this->match($forbiddenPhrases, $thought_content)
if ($word) {
    Flagable::create(['status' => '2', 'word' => $word, 'word_id' => '']);
}

如下更改您的函数:

private function match($needles, $haystack){
     foreach($needles as $needle){
         if (strpos($haystack, $needle) !== false) {
             return needle;
          }
     }
     return false;
 }
然后更改代码,如下所示:

 Public function store(PostRequest $request){ 
        $post = Post::create($request->validated());
        $warningWords = Word::where('type', 'warning_words')->pluck('content')->toArray();
        if(($word = $this->match($forbiddenPhrases, $thought_content)) !== false){
            Flagable::create('status' => '2', 'word' => $word, 'word_id' => '']);
        }
}

然后在控制器中,您可以执行以下操作:

$forbiddenPhrases = Word::where('type', 'warning_words')->pluck('content', 'id')->toArray();
$foundForbiddenPhrases = $this->match($forbiddenPhrases, $content);

if(count($foundForbiddenPhrases) > 0){

    foreach ($foundForbiddenPhrases as $phraseId => $foundForbiddenPhrase) {
        Flagable::create('status' => '2', 'word' => $foundForbiddenPhrase, 'word_id' => $phraseId]);
    }
}
$forbiddenPhrases = Word::where('type', 'warning_words')->pluck('content', 'id')->toArray();
$foundForbiddenPhrases = $this->match($forbiddenPhrases, $content);

if(count($foundForbiddenPhrases) > 0){

    foreach ($foundForbiddenPhrases as $phraseId => $foundForbiddenPhrase) {
        Flagable::create('status' => '2', 'word' => $foundForbiddenPhrase, 'word_id' => $phraseId]);
    }
}