View Full Version : Maintaining line breaks in MySQL?

07-12-2005, 09:09 PM
I'm looking for a way to maintain line breaks when submitting a form <textarea> in php/mysql. I know in ASP i was able to do a replace() on the requested form data to keep the line breaks.

can the same be done in PHP?

thanks in advance for your time!

07-12-2005, 09:26 PM
you don't have to do anything tp preserve line breaks. if the user enters linebreaks on your form then the they will be there in your form processor when you insert into the database.

be advised: different OS's have different ASCII chars for linebreaks:
unix = \n
windows = \r\n
mac = \r
in the incoming data will be different depending on the OS of the client.

07-12-2005, 09:38 PM
nl2br() (http://www.php.net/nl2br) This will convert all newlines to the <br /> tag

07-12-2005, 09:59 PM
oh kool!
and way less code then it took to do it in ASP :p

now my next question in reverse.

when bring the data back into a textarea for editing. how do i remove the <br /> and have just a blank space?

thanks again!

07-12-2005, 10:07 PM
// to repalce with spaces
$text = str_replace('<br />', ' ', $text);

// to replace with newlines
$text = str_replace('<br />', "\n", $text);

07-12-2005, 10:15 PM
kool, that works too.

i only have one problem now,
everytime i click edit on the form, and seems to add an extra space inbetween each line break. why is this happening?


07-12-2005, 10:28 PM
beacuse nl2br() does not remove the original linebreaks, it simply adds <br> tags (the actual line breaks are still there). consider this example:

if (isset($_POST['submit']))
$text = trim($_POST['text']);
$text = stripslashes($text);
$text = htmlspecialchars($text);

echo 'you entered:<br><br>' . nl2br($text);

echo '
<form action="" method="POST">
enter text:<br>
<textarea name="text">'; if (isset($text)) {echo ($text);} echo '</textarea><br>
<input type="submit" name="submit" value="submit">

08-03-2005, 09:10 PM
ok, i'm back on this again, and still have the same problem,

I don't understand the last example that devinemke showed me.
should this be used in the script that pulls the data, or the script that submits the data to the DB?

this is my code for pulls the data:

function GetContent($edit) {

$SqlHost = "localhost";
$SqlUser = "root";
$SqlPass = "sql";
$SqlDb = "National";

$link = mysql_connect($SqlHost, $SqlUser, $SqlPass) or die('Login details incorrect');
$db_selected = mysql_select_db($SqlDb, $link) or die('Database name incorrect');

$result = mysql_query("SELECT cData FROM content where cid='$edit'");
$row = mysql_fetch_array( $result );
#echo $row['0'];
$text = str_replace('<br />', "\n", $row['0']);
$text = trim($text);
$text = htmlspecialchars($text);
echo $text;



and it still pulls it with alot of spacing inbetween the each line break, and everytime i hit submit the spaces like double.

my code for submitting to the DB:

$content = nl2br($content);

the rest is just standard sql stuff.

how can I fix this?

thanks again for all your time!

08-03-2005, 09:35 PM
you should store the data in the database with the regular linebreaks (\n) characters. you should only use nl2br() when displaying the data in the browser.

08-03-2005, 09:45 PM
yes, thats what i'm doing.
but each time i edit a post and pull the data back down, it adds more line breaks.

so, what am i doing wrong??


08-03-2005, 09:49 PM
ok, i found the prob.

someone told me to use this when calling the text from the db.
$text = str_replace('<br />', "/n", $row['0']);

so i just removed the /n, and now it works correctly.

thanks for all your time and help!


08-03-2005, 09:54 PM
ok, let's back up. run the data thru nl2br() only when it's being displayed in the browser as regular text, NOT when it is within a pre-populated form field. within a form field just leave it with the actual linebreak characters. after your form is submitted run the data your form validation code (trim() and whatever else) and then run your UPDATE query.

in your GetContent() fucntion you posted above you are calling str_replace() to replace <br>'s with /n's which leads me to believe that you are storing <br>'s in the database (which you shouldn't).