2011-08-10 81 views
1

我有一個字符串將一行字符串換行並且不在末尾添加換行符?

$str = 'aaaaabbbbbcccccdddddeeeeefffffggggghhhhhjjjjj'; 

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

我不想在生產$str

aaaaa 
bbbbb 
ccccc 
ddddd 
eeeee 
fffff 
ggggg 
hhhhh 
jjjjj //No new line here 

回答

0

一種方式結尾處添加"\n"是while循環後SUBSTR換行符

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