0

I am leaning recursion and I want to create a search engine which depends on a user value and gets from an array all values which together make up the word that the user typed.

For example I have this array :

$array = array('it', 'pro', 'gram', 'grammer', 'mer', 'programmer');
$string = "itprogrammer";    

If anyone can help I appreciate it a lot. Thank you for your help.

1
  • Is your user interface a browser (see HTML form) or the console (see CLI or tty)? Commented Oct 4, 2018 at 5:10

1 Answer 1

1

Here is a recursive function that will do what you want. It loops through the array, looking for words that match the beginning of the string. It it finds one, it then recursively tries to find words in the array (excluding the word already matched) which match the the string after it has had the first match removed.

function find_words($string, $array) {
    // if the string is empty, we're done
    if (strlen($string) == 0) return array();
    $output = array();
    for ($i = 0; $i < count($array); $i++) {
        // does this word match the start of the string?
        if (stripos($string, $array[$i]) === 0) {
            $match_len = strlen($array[$i]);
            $this_match = array($array[$i]);
            // see if we can match the rest of the string with other words in the array
            $rest_of_array = array_merge($i == 0 ? array() : array_slice($array, 0, $i), array_slice($array, $i+1));
            if (count($matches = find_words(substr($string, $match_len), $rest_of_array))) {
                // yes, found a match, return it
                foreach ($matches as $match) {
                    $output[] = array_merge($this_match, $match);
                }
            }
            else {
                // was end of string or didn't match anything more, just return the current match
                $output[] = $this_match;
            }
        }
    }
    // any matches? if so, return them, otherwise return false
    return $output;
}

You can display the output in the format you desire with:

$wordstrings = array();
if (($words_array = find_words($string, $array)) !== false) {
    foreach ($words_array as $words) {
        $wordstrings[] = implode(', ', $words);
    }
    echo implode("<br>\n", $wordstrings);
}
else {
    echo "No match found!";
}

I made a slightly more complex example (demo on rextester):

$array = array('pro', 'gram', 'merit', 'mer', 'program', 'it', 'programmer'); 
$strings = array("programmerit", "probdjsabdjsab", "programabdjsab");

Output:

string: 'programmerit' matches:

pro, gram, merit<br>
pro, gram, mer, it<br>
program, merit<br>
program, mer, it<br>
programmer, it

string: 'probdjsabdjsab' matches:

pro

string: 'programabdjsab' matches:

pro, gram<br>
program

Update

Updated code and demo based on OPs comments about not needing to match the whole string.

Sign up to request clarification or add additional context in comments.

10 Comments

A pleasure. It was a really interesting problem.
by the way if only get 1 value from array it return error.
Do you mean if the array only has one value in it?
@IhsanDn the problem is your question only shows an example where the words match the string completely, so that is what I coded. I'll take a look at it but please try to put all information into the question at the beginning.
@IhsanDn see my revised code. Hopefully that will do what you want.
|

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.