Jump to content

[SOLVED] Taking a result and splitting into numerous lines


macinslaw

Recommended Posts

I am looking for a way to ake a result and split that result of varchar characters into 2 lines of 16 characters each.

 

For example, if I have the following as a result:

 

"My dog has fleas and ticks."

 

I would want it to display as:

 

"My dog has fleas"

" and ticks."

 

Any help would be appreciated.

 

-Mac

 

You could cycle through the string character by character, adding a new line every 16th character:

 

<?php
$str = "My dog has fleas and ticks.";
for($x=0;$x<strlen($str);$x++){
    $newstr .= $str[$x];
    if($x % 16 == 0 && $x != 0){
        $newstr .= '<br />';
    }
}
echo $newstr;
?>

 

Edit: If you are using PHP 5, you can use the str_split function:

 

<?php
$str = "My dog has fleas and ticks.";
$str = str_split($str,16);
foreach($str as $v){
    echo $v.'<br />';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.