Mercury Terminal
Mercury Terminal

Reputation: 109

PHP - Sub-string words of a string

I need to find substrings of a given string but the substrings must be a word in the English language.

I.E. Given string = every, then substrings will be "ever", "very" and etc.

Thanks for the help.

Upvotes: 0

Views: 1061

Answers (1)

linepogl
linepogl

Reputation: 9335

You will need two things. First, you have to find all possible substrings. Then you will need a list with all English words (there are many free compilations).

This is a possible implementation:

$result = array();
$len = strlen($string)
for ($i = 0; $i < $len; $i++) {
   for ($j = 1; $j <= $len - $i; $j++) {
      $substring = substr( $string , $i , $j );

      if ( is_an_english_word( $substring ) )
         $result[] = $substring;

   }
}

Upvotes: 2

Related Questions