Jump to content

string formatting


natalieG

Recommended Posts

Does anyone have a small piece of code that wil reformat a
string, by breaking it up into lines of a defined length, separated by
"<BR>" We have a string that can be uo to 300 charc=acters and
want to reformat into eight lines,or so.separated by <BR>.

Jennifer
Link to comment
Share on other sites

[code]<?php
$line_len=8; //change value to whatever line length you want
$len=strlen($string);
$lines=ceil($len/$line_len);
$arr=str_split($string, $line_len);
$i=0;
while($i<$lines){
echo($arr[$i]);
echo("<br>");
$i++;
};
?>[/code]

This should do it [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
$string is the string you got.
In this case I chose 8 chars per line.

Orio.
Link to comment
Share on other sites

so you are giving me a string whose length you describe in the number of characters it contains and you want me to break it into lines?

how many characters exist in a line?


i think i might have misunderstood your question.
show me an example of the input you would enter and the output you expect.


in any case, i believe you would find the functions [a href=\"http://www.php.net/substr\" target=\"_blank\"]substr()[/a] and [a href=\"http://www.php.net/strpos\" target=\"_blank\"]strpos()[/a] useful
Link to comment
Share on other sites

This will break it naturally at word boundaries


$txt = "Lorem ipsum dolor sit amet. Consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.";

[code]$size = strlen($txt);
$lines = 8;
$linesize = ceil($size/$lines);

echo wordwrap($txt, $linesize,'<BR>');[/code]

Gives
[code]Lorem ipsum dolor sit amet.
Consectetuer adipiscing elit, sed
diam nonummy nibh euismod
tincidunt ut laoreet dolore magna
aliquam erat volutpat. Wisi enim
ad minim veniam, quis nostrud
exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea
commodo consequat.
[/code]
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.