Reputation: 21
following:
I have a string which contains many spaces. I want to add a HTML Break "< br />" in the next space-char after every 70 characters of this string for layout reasons. this is how far i'm in php:
function news_break( $news_string ){
for( $i = 0; $i <= strlen($news_string); $i++ ){
if( $i % 70 == 0 ){ //Every 70th char
$c = TRUE;
}
if($c && $news_string[$i] == ' ' ){
//?? replace space with <br />
$c = FALSE; //Until the next 70th char is found
}
}
return $news_string;
}
I know the first run will be 71 characters because of the $i = 0, but afterwards he will set $c = TRUE; after every 70 following chars. Not that important because he is supposed to look after a space_char afterwards, and the probability that it is exactly on 71 is pretty low
Upvotes: 1
Views: 40
Reputation: 5739
you have to fill the replacement ... of course give a "parsed" string back
function news_break( $news_string ){
$out = '';
$c = false;
for( $i = 0; $i <= strlen($news_string); $i++ ){
if($i % 70 !=0 && $c===false)
$out.=$news_string[$i];
elseif( $i % 70 == 0 && $news_string[$i]!='' ){ //Every 70th char
$c = true;
$out.=$news_string[$i];
} elseif ( $i % 70 == 0 && $news_string[$i]=='')
$out.=$news_string[$i].'<br>';
elseif( $c=== true && $news_string[$i]=='') {
$out.=$news_string[$i].'<br>';
$c=false;
}
}
return $out;
}
Out of the box ... not tryed for errors ...
Upvotes: 0