Data that is stored in my mysql database, has the breaks of text input properly, but when retrieved the data no longer has the breaks, and everything is displayed as one string without any <br>. Any idea why this would occur?
The data column is the format text,
for example: in a table there is:
hey
how
do
you
do
After retrieving the data ill echo $mesarray[0][message]; and the result is:
hey how do you do
I want the line breaks to appear, but they dont.
The reason is because HTML does not understand line breaks. They need <br /> tags to break lines.
There is a function called nl2br() which can be used to convert new lines to <br>
echo nl2br($mesarray[0][message]);
Related
I have a serialized object stored in a mysql database in a column of type text saved via php.
If I double click the field in PHPMyAdmin to edit the value of the field inline, and then save the edit by clicking away from the edit box, the serialized data in the field is rendered corrupt somehow.
I get the following PHP error after doing this:
Notice: unserialize(): Error at offset 913 of 1951 bytes in /path/to/file.php on line 46
I am not even changing any of the data, as I am only clicking in to copy the data to the clipboard so it's not as though I'm introducing anything weird, or making an error with the syntax etc. I thought maybe some whitespace is getting added or some wierd character(s).
Is there a solution to this?
According to this question, in order to avoid errors you can try to base64 the output or serialize() before inserting it into the database:
$toDatabse = base64_encode(serialize($data)); // Save to database
$fromDatabase = unserialize(base64_decode($data)); //Getting Save Format
There is a bug in phpmyadmin that saves the '\n' of a serialized php string as line breaks. PHP serialized strings must not contain line breaks! If you want to safely modify it directly from phpmyadmin, make sure to replace the line breaks with '\n' string, this can easily be done from any text editor.
I’m converting a number of html documents into csv files to upload into MySQL – I’m replacing html div’s etc with tabs as necessary in a text editor to get the data into the columns, then pasting into a spreadsheet and saving as CSV. This is working fine
A couple of ‘fields’ have data on more than one line, which in html is achieved by using br’s, leaving these in the CSV displays the data in the required format.
I’ve also produced a script to add/amend data and for the multi line fields I’m using textareas and then outputting using nl2br() which again displays the data as required. But the uploaded data with br’s is displayed in the add/amend script as a continuous line with the br’s.
Question – Is there anything I can do when manipulating the html data to replace the br’s so that they appear as multi lines in the textareas ?
Replace with "\n"
<?
$string="Some stuff <br/> some other";
$string=str_replace("<br/>","\n",$string);
?>
<textarea><?=$string?></textarea>
You can replace with \n instead of br tag
Some strange behaviour with the same script on different Websites here. Here you go:
I have an textarea where users enter text. They also do newlines using enter. That is stored inside the mysql db like this:
Line One\r\nLine Two\r\n\r\nLine Three
The problem occures when displaying that text inside a textarea again.
In most cases everything works fine. The following is displayed inside the textarea:
Line One
Line Two
Line Three
On some websites however only the following is displayed inside the textarea:
Line One
Line Two
Line Three
Inside the db the two newlines (\r\n\r\n) are still there. Only one is displayed however.
This happens like i said only on some websites, in most cases multiple newlines like this \r\n\r\n are displayed fine.
UPDATE:
Seams like JSON has something to do with it. It is an JSON string containing a string like "Line One\r\nLine Two\r\n\r\nLine Three".
{"mystring":"Line One\r\nLine Two\r\n\r\nLine Three"}
$row = $db->load(); // get the json string
var_dump($row); // \r\n\r\n still inside!
$jrow = json_decode($row);
var_dump($jrow->mystring); // on some servers multiple newlines are chopped to one
Try using n2lbr when displaying the DB value in the textarea. Does that help at all? It's worked for me in the past.
I'm getting data from my textarea with the following code
$about_me=mysql_real_escape_string(nl2br($_POST['about_me']));
which
1. Receives data, using $_POST.
2. nl2br makes brakes so If I echo this code to user he will see if there were new lines.
3. mysql_real_escape_string to secure code from mysql injections before entering it to database.
So if I echo this code everything works fine.
But If I edit it again through textarea, php goes to mysql gets data, puts it to textarea and I see <br> signs...
How can I get rid of them while editing my text again in textarea ?
How can I get rid of them while editing my text again in textarea ?
Stop using nl2br(), of course. It's entirely wrong here.
You use nl2br() when you want to output data that contains linebreaks to HTML, not when you want to store it in the database. Store data unchanged, format it for viewing.
If you output it into a <textarea> you don't need to use it either, since textareas display linebreaks (whereas HTML in general does not). For the textarea you need htmlspecialchars(), but apparently this is already happening - otherwise you would not see literal <br> showing up.
<?php
function br2nl($string){
$return=eregi_replace('<br[[:space:]]*/?'.
'[[:space:]]*>',chr(13).chr(10),$string);
return $return;
}
?>
Use this while getting data from database and before printing into textarea .
http://php.net/manual/en/function.nl2br.php
Check examples on this page
so I need the user to write a review about an article or book and send it to a DB via PHP but with some basic HTML formatting.. I mean, I have a form , when the user writes the review, the data is sent but without any kind of formatting, If the user want to write in a new line, the text is sent like plain text, I need to get also those new line breaks and simple stuff.
I know how to use PHP and DB connection, I just need to know how to get those new line breakes and stuff..
Use nl2br
Just before printing on the screen data from DB. It replaces \n (new line) as <br>
I recommend storing the data as plaintext, and adding the formatting on the way out. This way if you want to change the way it is formatted then you don't have to update every row in the database.
you can use nl2br() if you just need to newlines to be formatted, and a search-and-replace for anything else.
Have you considered using an existing 'plain text to markup' solution, like Markdown?
It (and others like it) allow your users to write plaintext reviews that will be sensibly formatted. (like stackoverflow uses!)
The PHP function nl2br() basically takes every new line your user enters via the form and converts the new line code to a <br> tag.
An example of using this would be:
$text = nl2br("This is text \nThis is a new line of text");
This would create the following code in your database:
This is text<br>This is a new line of text
When the user hits enter in the form textarea, PHP will pick this up as \n.