0

When someone writes an entry in the textbox, I want to check the words that they used and find a match in a common word DB that I have. Each word in the DB had a number value that I need it to return.

I need to write an if statement that checks if a query returns a value, and if it does I need it to return -1.

This is what I have so far but right now but it only returns one word.

    Entry::create($request->all());

    // GETTING THE ARRY OF WORDS FROM entry_body( $entry_text)
    $entry_text = $request->only('entry_body');

    // REMOVE THE KEY AND JUST GET THE STRING VALUE
    $entry_text = array_pull($entry_text,"entry_body");
    $entry_noTag = strip_tags($entry_text);

    // CONVERTE THE STRING ARRAY
    $entry_explode = explode(" ", $entry_noTag);

    // EACH WORD GETS ANALAYZED 
    $matched_words = Words::whereIn('word', $entry_explode)->get();

    foreach ($entry_explode as $word) {
        if ($matched_words == false){
            return '-1';
        }
        else {
            return $word;
        }
    };

I can get the code to return the matched values. What I need it to do is when a word in the array is not matched, it should return -1 so I know the word was not found in the DB.

1 Answer 1

1

Your first return will, of course, return and end the function. Put all values in array like this:

$arrayOfWords = array();
foreach ($matched_words as $word) {
    if (in_array($word->word,$entry_explode)) {
        $arrayOfWords['found'][] = $word->word;
    } else {
        $arrayOfWords['notFound'][] = $word->word;
    }
}

return $arrayOfWords;
Sign up to request clarification or add additional context in comments.

3 Comments

Got it.But I still can not get the other words that are not being matched to return.
I can get the code to return the matched values. What I need it to do is when a word in the array is not matched, it should return -1 so I know the word was not found in the DB.
I've made a last edit. Now, you have a array with all words found and not found, just do whatever you need to do with those words.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.