Eiolon Posted December 12, 2006 Share Posted December 12, 2006 I have people entering data in a text field and they use line breaks for paragraphs. Unfortunately, when I call the data up from the database it is all run together. Is there a way to make it so the line breaks stay? Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted December 12, 2006 Share Posted December 12, 2006 When you say line breaks do you mean \n? And when you say "call data up from the database" do you mean in a web browser? If so, you're in luck http://php.net/nl2br ;). Quote Link to comment Share on other sites More sharing options...
monkey_05_06 Posted December 12, 2006 Share Posted December 12, 2006 [code]<?php$text = (get_magic_quotes_gpc() ? stripslashes($_POST["text"]) : $_POST["text"]);$text = mysql_real_escape_string(nl2br($text));?>[/code]stripslashes and mysql_real_escape_string are there to prevent malicious input. nl2br is the function you needed. It converts new-line characters (\n) to HTML line-breaks (< br />).[b][EDIT:][/b]utexas_pjm posted before me...but...same answer. Quote Link to comment Share on other sites More sharing options...
Michan Posted December 12, 2006 Share Posted December 12, 2006 I was just working on this exact function. Here's my take.When you actually post the item to the database/email/page/wherever, use the following code:[code]$string = str_replace("\n", "<br>", $string);[/code]Replace $string with the name of the text area. Quote Link to comment Share on other sites More sharing options...
Eiolon Posted December 12, 2006 Author Share Posted December 12, 2006 Thank you all. This appears to be what I am looking for. Quote Link to comment Share on other sites More sharing options...
monkey_05_06 Posted December 12, 2006 Share Posted December 12, 2006 Michan that's precisely what PHP's built-in nl2br function does. But good thinking!Just FYI though HTML now requires (for the standard though most browsers will accept the old method) all tags that don't have a closing tag, i.e., < br />, < hr />, < img />, etc., to end with "/>" instead of just ">". That signifies the closing of these single-tag tags. ;) Quote Link to comment Share on other sites More sharing options...
boby Posted December 12, 2006 Share Posted December 12, 2006 If you really want to use [b]str_replace[/b] then I suggest you replace also [b]\r[/b] and [b]\r\n[/b].[code=PHP]$string = str_replace (array ("\r\n", "\n", "\r"), "<br />", $string);[/code][b]nl2br[/b] will take care of all three ways.[b]\n[/b] = *Nix[b]\r[/b] = Windows[b]\r\n[/b] = Mac 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.