Zergman Posted August 3, 2008 Share Posted August 3, 2008 I read through some previous posts regarding nl2br and I just don't get it. I did learn that its advised to use nl2br outputting the data, not inputting. Formatting seems correct in the database for the entered text so it looks like its going in right. I just can't figure out how to use nl2br. Don't even know what code to post here lol. Link to comment https://forums.phpfreaks.com/topic/117963-solved-nl2br-help/ Share on other sites More sharing options...
PHPTOM Posted August 3, 2008 Share Posted August 3, 2008 For example. If you have used mysql_fetch_array assigned to $info You do: echo nl2br($info[field]); Whever you echo it, wrap it in nl2br ---------------- Now playing: Linkin Park - Hands Held High via FoxyTunes Link to comment https://forums.phpfreaks.com/topic/117963-solved-nl2br-help/#findComment-606818 Share on other sites More sharing options...
papaface Posted August 3, 2008 Share Posted August 3, 2008 Basically you use this to format the text in the way it was originally formatted in terms of new lines. When data is inserted into a db it is entered with new lines as \n . nl2br allows you to replace the \n with <br /> which creates a new line in HTML. Link to comment https://forums.phpfreaks.com/topic/117963-solved-nl2br-help/#findComment-606820 Share on other sites More sharing options...
unkwntech Posted August 3, 2008 Share Posted August 3, 2008 A good place to start for any function is the PHP manual http://php.net/functionName in this case http://php.net/nl2br Link to comment https://forums.phpfreaks.com/topic/117963-solved-nl2br-help/#findComment-606821 Share on other sites More sharing options...
genericnumber1 Posted August 3, 2008 Share Posted August 3, 2008 Long explanation: nl2br() just changes all the line breaks (\n) V line break ^ line break Into <br /> html tags... So when, for instance, someone enters some text into a form, a good script would put that data as-is into a database (assuming it's secure). Then, later, a different script would query the database, and when displaying it, it would nl2br() the data so that the line breaks are displayed in the html. (line breaks don't show up on the rendered end of html without <br /> tags). For example one script inserts a blog post <?php $title = $_POST['title']; $body = $_POST['body']; // escape the quotes here mysql_query("INSERT INTO news (title, body) VALUES ('$title', '$body')"); ?> And another script gets the info out and uses nl2br() <?php $resource = mysql_query("SELECT * FROM news"); while($row = mysql_fetch_assoc($resource)) { $row['body'] = nl2br($row['body']); echo "TITLE: {$row['title']} <br />"; echo "BODY: {$row['body']}"; } ?> So a body post in the above script of This is a test post. Hi! would become This is a test post.<br />Hi! Link to comment https://forums.phpfreaks.com/topic/117963-solved-nl2br-help/#findComment-606823 Share on other sites More sharing options...
Zergman Posted August 3, 2008 Author Share Posted August 3, 2008 Good stuff! I did check the php manual, but it went WAY over my head. Didn't make too much sense to me ??? So if I understand this right, when I echo the information I want formatted Ex <td><?php echo $row_rsexdisplay['notes']; ?></td> And this is me taking a wild stab in the dark <?php echo nl2br($row_rsexdisplay['notes']); ?> Do i have it right? Link to comment https://forums.phpfreaks.com/topic/117963-solved-nl2br-help/#findComment-606828 Share on other sites More sharing options...
unkwntech Posted August 3, 2008 Share Posted August 3, 2008 Yap, you got it. Link to comment https://forums.phpfreaks.com/topic/117963-solved-nl2br-help/#findComment-606830 Share on other sites More sharing options...
Zergman Posted August 3, 2008 Author Share Posted August 3, 2008 yay me! You all rock, many many thanks! Wicked stuff, thanks all! Link to comment https://forums.phpfreaks.com/topic/117963-solved-nl2br-help/#findComment-606837 Share on other sites More sharing options...
cooldude832 Posted August 3, 2008 Share Posted August 3, 2008 that is fairly straightforward just remeber that nl2br isn't what it says it is in the title it sounds like New Line to html Break However its really Insert html Break before New Lines So running nl2br(nl2br($var)); will produce double breaklines since it doesn't destroy the \n or \r present Link to comment https://forums.phpfreaks.com/topic/117963-solved-nl2br-help/#findComment-606858 Share on other sites More sharing options...
unkwntech Posted August 3, 2008 Share Posted August 3, 2008 @cooldude832 - thats a good note. If your just looking to replace \n or \r with a < br /> you can use this echo preg_replace('/(?:\n|\r)/i', '<br />', $row_rsexdisplay['notes']); Link to comment https://forums.phpfreaks.com/topic/117963-solved-nl2br-help/#findComment-606863 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.