2013-06-19 58 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

相關問題