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

 

Link to comment
Share on other sites

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 />';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.