This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Newline Conversion in Submitted Text in PHP
I'm using PHP to store text submitted from a textarea into a database to later be echoed out. If a user presses enter in the textbox to start a new line, I want it to start a line break.
How can I go about doing that?
Let's say this is the textbox:
This is line one.
This is line two.
When a user submits this to the database it is just stored as one line and echoed out is the same result.
"This is line one. This is line two."
So how do I make the stored text reflect the format of the original input?
You would run something like nl2br() on your output when reading FROM the database. The new lines will be stored as \n.
http://php.net/manual/en/function.nl2br.php
It should also be noted that you should not run nl2br() before inserting into the database. Just not a good practice.
Related
I have a form with a textarea whose results are inserted into a mysql database. I'm using nl2br to preserve the line breaks.
However, because this inserts br's in the text, when a user goes to edit what they've entered in the textarea, it shows all the br's in the textarea which were saved in mysql (looks ugly for people who don't know html).
So, if I don't use nl2br, the line breaks look nice when echoed back in the textarea but not saved in the database correctly. If I use nl2br, the line breaks are saved in the database correctly but look ugly when echoed back. How do I echo the saved textarea contents back onto the page without showing the br's all over the place?
Store the test in the database with break lines and when you echo it out to the screen run it through nl2br.
This question already has an answer here:
Escape line breaks in MySQL output
(1 answer)
Closed 9 years ago.
The column data is fed by a textarea html element in the web page ; so the user can enter linebreaks within it. When I put the column data inside an excel file then excel does not recognize the linebreak ( there is a "?" at the end of the first line ). So how to make it recognizable by excel ?
I ran into this problem a little earlier, The easiest solution (Not the best, but couldn't think of anything else) is to use str_replace().
The only way i found to get this to work correctly, was to replace \n with \n\r. An example of this would be
<?php
str_replace("\n", "\n\r", $input_text);
?>
It is possible to do this while inserting into the database, or while reading from the database, However it does present a problem that if it already has \n\r, it'll then appear like \n\r\r
I have a form with a textarea whose results are inserted into a mysql database. I'm using nl2br to preserve the line breaks.
However, because this inserts br's in the text, when a user goes to edit what they've entered in the textarea, it shows all the br's in the textarea which were saved in mysql (looks ugly for people who don't know html).
So, if I don't use nl2br, the line breaks look nice when echoed back in the textarea but not saved in the database correctly. If I use nl2br, the line breaks are saved in the database correctly but look ugly when echoed back. How do I echo the saved textarea contents back onto the page without showing the br's all over the place?
Store the test in the database with break lines and when you echo it out to the screen run it through nl2br.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Insert into database problem… (Bad character coding) PHP/MYSQL
I have a form which is submitted into a mysql database. The database is set to UTF-8_GENERAL and the rows are using the same character coding as well.
But when I submit the form with a "ő" or "ű" in the text, it does not submit anything after these characters. (Example: "This is a nice ű day." It just inserts this into the db: "This is a nice")
The form validation page has the mysql_real_escape_string(); strip_tags(); before submitting to the db.
I echo out the result after every string function (mentioned above). It all works fine, but when it gets inserted, it doesn't display anything after those characters.
[MySql version: 5.0.51a-24+lenny5-log | Using phpMyAdmin version: 2.11.8.1deb5+lenny7]
How could I solve this? Any help appreciated...
encode the characters:
$string = mb_convert_encoding($variable, 'HTML-ENTITIES', 'UTF-8');
Quick and simple (I hope) question; If a user inputs new lines in a text field, ie:
43 Dennis
Beeston
How can I save the new line rather than have it transferred to the mysql server as one line?!
I am using PHP and mysql.
When you output the field from the database to a html document, either use <pre> or nl2br() (or a <textarea>).
To save the newlines in the database, take a look at mysql_real_escape_string
If you want your newlines displayed as breaks on the page when you display the records, use nl2br.
MySQL should preserve the newline - could it be your output (or your mysql gui) that isn't displaying it?