将字符串爆炸成相等的部分

时间:2011-05-30 07:04:49

标签: php arrays explode

$str =  "Hello fri3nd, you're looking good today!  What a Great day this is!  How     fancy and cool!";
$pieces = explode(" ",$str, 3);

print_r($pieces);

所以这给了我$pieces[0] = 'Hello', $ pieces [1] ='fri3nd'... and the rest of the string is all shoved into $ pieces [3]`。如何在每3或4个单词中爆炸?

2 个答案:

答案 0 :(得分:2)

也许:

<?php
$str =  "Hello fri3nd, you're looking good today!  What a Great day this is!  How     fancy and cool!";

$array = array_map(create_function('$a', 'return implode(" ", $a);'), array_chunk(preg_split('/\s+/', $str), 3));
var_dump($array);

说明:

答案 1 :(得分:1)

使用php str_split功能: -

$pieces = str_split( $str, 3);