开发者

PHP - Sub-string words of a string

开发者 https://www.devze.com 2023-02-16 15:13 出处:网络
I need to find substrings of a given string but the substrings must be a word in the English language.

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 e开发者_运维百科tc.

Thanks for the help.


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;

   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消