natalieG Posted June 24, 2006 Share Posted June 24, 2006 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 andwant to reformat into eight lines,or so.separated by <BR>.Jennifer Quote Link to comment https://forums.phpfreaks.com/topic/12826-string-formatting/ Share on other sites More sharing options...
Orio Posted June 24, 2006 Share Posted June 24, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/12826-string-formatting/#findComment-49182 Share on other sites More sharing options...
Koobi Posted June 24, 2006 Share Posted June 24, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/12826-string-formatting/#findComment-49183 Share on other sites More sharing options...
Barand Posted June 24, 2006 Share Posted June 24, 2006 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, seddiam nonummy nibh euismodtincidunt ut laoreet dolore magnaaliquam erat volutpat. Wisi enimad minim veniam, quis nostrudexerci tation ullamcorper suscipitlobortis nisl ut aliquip ex eacommodo consequat.[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12826-string-formatting/#findComment-49197 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.