开发者

echo text separating every 3 words per line?

I have seen some samples using var_dump, but I I would rather use a simple echo, if it's possible.

It should look like this using echo:

This is a
simple text
I just wrote

Using var_dump:

function split3($text)
{
    $array = array();
    foreach(explode(' ',$text) as $i=>$word)
    {
        if($i%3) {
           开发者_Python百科 $array[floor($i/3)] .= ' '.$word;
        } else {
            $array[$i/3] = $word;
        }
    }
    return $array;
}

$text = "This is a simple text I just wrote";
var_dump(split3($text));


Your sample output is a bit wrong compare to your question.

If the output is like this.

This is a
simple text I
just wrote

Then replace the var_dump(split3($text)); with this

$splitedText = split3($text);
foreach($splitedText as $value){ //Just print the array content
    echo $value . "<br />";  //I use <br /> as a new line   
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜