将一行换成新行并且不要在末尾添加换行符?

时间:2011-08-10 10:08:51

标签: php

我有一个字符串

$str = 'aaaaabbbbbcccccdddddeeeeefffffggggghhhhhjjjjj';

$pos = 0;
$tmp = 0;
while($pos<strlen($str)) {
  $tmp  .= substr($str,$pos,5)."\n";
  $pos += 5; 
}

我不想在制作"\n"

的末尾添加$str
aaaaa
bbbbb
ccccc
ddddd
eeeee
fffff
ggggg
hhhhh
jjjjj //No new line here

3 个答案:

答案 0 :(得分:3)

echo join("\n", str_split($string, 5));

http://www.php.net/str-split
http://www.php.net/join

答案 1 :(得分:1)

while($pos<strlen($str)) {
  if ($pos > 0) $tmp .= "\n";
  $tmp .= substr($str,$pos,5);
  $pos += 5; 
}

答案 2 :(得分:0)

一种方法是在while循环之后对换行进行索引