2013-06-19 68 views
-2

如何将任意长度的句子切成5个字的数组?如何将一个大数组切片成较小的数组

换句话说:

$Dummy = "Here is a long sentence, but what I need to make out of this sentence 
      is that it should be chunked off to smaller arrays with 5 words each." 

我想在PHP的答案。

预先感谢解决它的人。

+0

请发表您的代码到目前为止,并解释什么是行不通的。 – elclanrs

+0

不是很确定的要求,但尝试'爆炸()' – swapnesh

+0

可能的重复:[我如何只获得一个确定数量的字从字符串在PHP?](http://stackoverflow.com/ q/1112946/367456) - 和其他许多人。请使用搜索。 – hakre

回答

7

请参阅array_chunk()

$words = str_word_count($sentence, 1); 
$chunks = array_chunk($words, 5); 
+1

不期待'str_word_count'返回真正的单词....好奇的功能。 – mpen

+1

而不是str_word_count,你也可以使用爆炸函数。 $ words = explode(“”,$ sentence); str_word_count不适用于数字字符,例如,如果没有明确指定并将它们视为分隔符。 – Fallen

+0

@MuhammedHedayet,'explode()'会为“双空间”留下空的元素。一切都有其局限性。还要考虑'array_filter()'。 – BlitZ

相关问题