我有一个PHP字符串,其中包含英语单词。我想从字符串中提取所有可能的单词,而不是由explode()通过空间提取,因为我只有一个单词。我的意思是从一个词中提取单词。
示例:和单词"stackoverflow",我需要全部提取stack, over, flow, overflow。
我正在使用pspell_check()进行拼写检查。我目前正在得到以下组合。
--> sta
--> stac
--> stack
and so on.因此,我找到了唯一匹配stack的单词,但我想找到以下单词。注意,我不想要最后一个词,因为我已经说过了。
--> stack
--> over
--> flow我的代码:
$myword = "stackoverflow";
$word_length = strlen($myword);
$myword_prediction = $myword[0].$myword[1];
//(initial condition as words detection starts after 3rd index)
for ($i=2; $i<$word_length; $i++) {
$myword_prediction .= $myword[$i];
if (pspell_check(pspell_new("en"), $myword_prediction))
{
$array[] = $myword_prediction;
}
}
var_dump($array);发布于 2015-07-30 20:18:17
如果你有这样的外圈怎么样。第一次从$myword的第一个字符开始。第二次从第二个字符开始,依此类推。
$myword = "stackoverflow";
$word_length = strlen($myword);
$startLetter = 0;
while($startLetter < $word_length-2 ){
$myword_prediction = $myword[$startLetter] . $myword[$startLetter +1];
for ($i=$startLetter; $i<$word_length; $i++) {
$myword_prediction .= $myword[$i];
if (pspell_check(pspell_new("en"), $myword_prediction)) {
$array[] = $myword_prediction;
}
}
$startLetter ++;
}发布于 2015-07-30 20:19:43
那么,您需要获取所有的子字符串,并检查每个子字符串:
function get_all_substrings($input){
$subs = array();
$length = strlen($input);
for($i=0; $i<$length; $i++){
for($j=$i; $j<$length; $j++){
$subs[] = substr($input, $i, $j);
}
}
return array_unique($subs);
}
$substrings = get_all_substrings("stackoverflow");
$pspell_link = pspell_new("en");
$words = array_filter($substrings, function($word) use ($pspell_link) {
return pspell_check($pspell_link, $word);
});
var_dump($words);https://stackoverflow.com/questions/31732949
复制相似问题