Jump to content

string formatting


natalieG

Recommended Posts

[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
https://forums.phpfreaks.com/topic/12826-string-formatting/#findComment-49182
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
https://forums.phpfreaks.com/topic/12826-string-formatting/#findComment-49183
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
https://forums.phpfreaks.com/topic/12826-string-formatting/#findComment-49197
Share on other sites

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.