0

php有什么简单的方法可以用1-3个词来爆炸句子吗?

$str = 'Wikipedia was launched on January 15, 2001.';
$str = preg_replace('/[[:punct:]]/i',' ',$str);
$parts = explode(' ',trim($str));

现在我想说出这样的话。谢谢。

$output_array={
"Wikipedia",
"was",
"launched",
"on",
"January",
"15",
"2001", //one word
"Wikipedia was",
"launched on",
"January 15",
"was launched",
"on January",
"15 2001", //two nearby words.
"Wikipedia was launched",
"on January 15",
"was launched on",
"January 15 2001",
"launched on January",
"on January 15" //three nearby words.
}
4

1 回答 1

2
$parts = explode(' ',preg_replace('/[[:punct:]]/i','',$str));
$output_array = array();
for ($words = 1; $words <= $max; $words++)
    for ($wordIndex = 0; $wordIndex <= count($parts) - $words; $wordIndex++)
        $output_array[] = implode(" ", array_slice($parts, $wordIndex, $words));

这正是你想要的。与$max附近的单词的最大值。(您可能希望将其设置为count($parts)

它遍历数组并添加到$output_array由空格内爆的单词数组。

于 2013-04-24T15:58:49.557 回答