Jump to content

Archived

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

natalieG

string formatting

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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.