PHP中的短语拆分算法

前端之家收集整理的这篇文章主要介绍了PHP中的短语拆分算法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
不知道如何解释.我们来举个例子吧.说我想分开这句话

“今天是个好日子.”

today
today is
today is a
today is a great
today is a great day
is
is a
is a great
is a great day
a
a great
a great day
great
great day
day

这个想法是在一个句子中得到所有顺序组合.

我一直在想什么是PHP中最好的方法.欢迎任何想法.

这是一个例子:
$sentence = 'Today is a great day.';

// Only leave "word" characters and whitespace
$sentence = preg_replace('/[^\w\s]+/','',strtolower($sentence));

// Tokenize
$tokens = explode(' ',$sentence);

for($i = 0; $i < count($tokens); $i++) {
    for($j = 1; $j <= count($tokens) - $i; $j++) {
        echo implode(' ',array_slice($tokens,$i,$j)) . "<br />";
    }
}

输出

today
today is
today is a
today is a great
today is a great day
is
is a
is a great
is a great day
a
a great
a great day
great
great day
day
原文链接:https://www.f2er.com/php/130420.html

猜你在找的PHP相关文章