Hi.
In my CMS, when i type a content for a new post in the textarea, like this:

one
two
three
four
five
six

and then click the submit button, it will save in the db table like the way i have typed, every word in a separate line.
But when the script echo the post on the page, i see all words are in one line like this:

one two three four five six

Why? Do i have to type tags like <br> while typing the content on the text area?! But users must feel free to write their posts without thinking about using html tags while they are typing!

RikTelner commented: Good question, I'd like to know too. +2

use a <pre> tag to output the text as typed

or just replace all newline chars with <br/>

I don't know about the idea of using <pre> tag, but normally what I done is, when post the textarea content to php, I tempt to use nl2br() to make sure the things stored to databased is already having the line break tag which makes my display part easier as you will never know if user enter

one two
three four
five

or

one
two
three
four
five

I read here and here, but would you please give me a simple example of how to use it in a text? Like what we see in the second link i pointed?

It means users should use and type |nl2br while they are typing their text in the textarea themselves?

Ok, here is what I would have done:

<?php
$data = nl2br(htmlentities($_POST['textarea']));
//put the $data into database
?>

With this, you can see the database data will be one<br />two<br />three<br />four if user entered
one
two
three
four
while it will showing one two<br />three four if user insert
one two
three four

with these, when we do the select statement, we do not need to process anymore, just simply use the data from database and direct use it as html. But if you wish to assign values back to the textarea, remember to str_replace the <br /> back into \n.

With this, you can see the database data will be one<br />two<br />three<br />four if user entered
one
two
three
four
while it will showing one two<br />three four if user insert
one two
three four

Isn't it it's purpose? It changes new lines into <br />'s. nl2br() will behave dependant on user's input. Depending on which output you wanna get, you need to watch out what user can type in.

If you want

one two
three four

turn into:

one
two
three
four

You need to change spaces into <br />'s.

@RikTelner, what are you wish to achieve with your post above?

@lps, with using $content = nl2br(htmlentities($_POST['content'])); in my script, i can type a text in the textarea and use ENTER key for line breaking. Then when i send the content into db table, data will be saved there with <br> tags.
But when i want to edit that content...
The script gets data from db table and show it in the textarea again for editing. So at this time i see the text with <br> tags in it and if i add a new sentence to the text and click the submit button, data will be updated in the db, and after that when i echo the post on the page for reading, the text contains <br> tags.
How should i solve it?

But if you wish to assign values back to the textarea, remember to str_replace the <br /> back into \n.

Would you please explain it more clear?
Is it about the problem i pointed above?

Yes, the reason for adding the 'But if you wish to assign values back to the textarea, remember to str_replace the <br /> back into \n.' is to prevent the situation what you faced now.
When inserting data, use nl2br(htmlentities($_POST['textarea'])); When put data into textarea for editing, use<textarea><?php echo str_replace("<br />","\n",$data); ?></textarea>`
When display to html, just simply echo $data.

This is what i typed in the textarea for a new post and inserting into db with $content = nl2br(htmlentities($_POST['content'])); :

hi
hi
hi

Then i checked db table and it was like this:

hi<br />
hi<br />
hi

Then i checked to see how the script would echo the post on the page and it was:

hi
hi
hi

It seems to be ok but...
After that i went to edit the post.
When i clicked the edit button and entered into edit page, the content of the textarea was like this:
(with Content: <textarea name="content" rows="10" cols="40"><?php echo str_replace("<br />","\n",$row['Content']); ?></textarea>)

hi

hi

hi

It was not what i wanted to face with but anyway i edited:

hi

hi

hi //here i press the Enter key once and add a new word.
welcome

I checked db table again and it was like this:

hi<br />
<br />
hi<br />
<br />
hi<br />
welcome

I checked to see how the script would echo it on the page:

hi

hi

hi
welcome

And finally when i wanted to edit it again the textarea was like this:

hi



hi



hi

welcome

Oh dear, please do test and try to solve these small problem by yourselves next time. Don't expect people done all the job for you, we are just here for help, not for job.

do some research why it is not working maybe? I googled for some and found that nl2br() only changes \n to <br> and does not account for Mac or Windows line breaks which are \r and \n\r respectively. So, alternatively, change the codes into
$data = ereg_replace("(\r\n|\n|\r)", "<br />",$text);, and <textarea"><?php echo preg_replace('/\<br(\s*)?\/?\>/i', PHP_EOL, $data); ?></textarea/>

Thank you @lps, it works ok now with using Content: <textarea name="content" rows="10" cols="40"><?php echo str_replace("<br />","",$row['Content']); ?></textarea> I think you have edited your last post.

$data = ereg_replace("(\r\n|\n|\r)", "<br />",$text);, and <textarea"><?php echo preg_replace('/\<br(\s*)?\/?>/i', PHP_EOL, $data); ?></textarea/>

I tested the code above but it doesn't work well, has the same problem i faced with last time.

I know here is not the place to say that but:
Sorry. I don't expect people here doing all the job for me at all. I dont post my problems here in the forum to get the correct code to copy and paste in my script. As i have mentioned before in my some topics, i need people's guidance not the exact answers. Every time i face with a new problem i try alot to find the way and do it myself, if i don't find the solution finally, i post my question here to get guidance. Sometimes i have problem even with the code people gave me and i don't find how to correct it myself so i have to ask.
My last question was small problem for you and maybe for many of members but not for me. I just want to learn. (LEARNING is more important than creating that cms for me.)

...anyway, thank you for your all answers and helps.

Maybe you mistook my meaning of previous code. What I meant is instead of using

<?php
$data = nl2br(htmlentities($_POST['textarea']));
//put the $data into database
?>

use

<?php
$data = ereg_replace("(\r\n|\n|\r)", "<br />",$text);
//put the $data into database
?>

as for the concern of nl2br() only changes \n to <br> and does not account for Mac or Windows line breaks which are \r and \n\r respectively.

Then your data in database should not be having line break, it should just showing one<br />two<br />three

And use the <textarea"><?php echo preg_replace('/\<br(\s*)?\/?\>/i', PHP_EOL, $data); ?></textarea/>

I know! i changed exactly those 2 parts you are pointing at them, but it didn't solve the problem. I changed them correctly but they didn't work well.
I soved it by using <?php echo str_replace("<br />","",$row['Content']); ?>.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.