proctk Posted September 21, 2007 Share Posted September 21, 2007 Hi I have a DB with a column that is set as Text format. The data stored in the field will be stored in many lines. When I get the data from the DB I want it to display where the line breaks are I'm not sure how to write the script to do this. I have worked withe explode before but there is nothing come at the end of each line to do this with. any help is excellent Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 21, 2007 Share Posted September 21, 2007 Line breaks are generally known as a \n character so try mainiuplating the string by exploding at the \n or find the position of htem, whatever floats yoru boat. Quote Link to comment Share on other sites More sharing options...
proctk Posted September 21, 2007 Author Share Posted September 21, 2007 here is what fixed my problem <?php echo nl2br($recipe['preperation']); ?> My next question s how would I add a number in front of each new line Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 21, 2007 Share Posted September 21, 2007 First are you looking for a constant or a counting number (like Line 1, Line 2, Line3) secondly, since nl2br is converting all the \n to <br /> lets explode the string at the "<br />"; then say foreach($exploded as $value) echo $i: $value; $i++; something like that (its psedeo code not real stuff) as long as you don't have a lot of lines (like more than 100) Quote Link to comment Share on other sites More sharing options...
proctk Posted September 21, 2007 Author Share Posted September 21, 2007 thank you for the post, My newbie ness is having a hard time figuring out how to put it togther, could you post an example Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 21, 2007 Share Posted September 21, 2007 Instead of running the nl2br, just do this: <?php $lines = explode("\n", $recipe['preperation']); foreach($lines AS $num=>$line){ print $num.' '.$line.'<br/>'; } ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 21, 2007 Share Posted September 21, 2007 <?php $string = nl2br($fromsql); $exploded = explode("<br />",$string); $i = 0; foreach($exploded as $value){ echo $i.": ".$value."<br />"; $i++; } ?> $fromsql is the output from mysql in this case. Quote Link to comment Share on other sites More sharing options...
proctk Posted September 21, 2007 Author Share Posted September 21, 2007 thank you all for the replies thats excellent. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 21, 2007 Share Posted September 21, 2007 by running nl2br on it before exploding, you have a line that isn't needed...why convert it to something else, to just explode it on that? Quote Link to comment 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.