LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP+SQL : formatting text for presentation in a blob (https://www.linuxquestions.org/questions/programming-9/php-sql-formatting-text-for-presentation-in-a-blob-258572/)

caged 11-23-2004 08:08 PM

PHP+SQL : formatting text for presentation in a blob
 
Hello.

I have some PHP and SQL pages where users can compose messages and save them.

The users type in their text into a html textarea and then PHP saves the string into a longblob type field in the SQL database.

My problem is this means all formatting like line breaks and spaces are lost.

How can i have the message understand where line breaks are and so forth without having the user type in the html code them self?

I know it must be possible since the LQ.org box i am typing in at the moment seems to have this ability.

do i need to use a client-side scripting language?

cheers,
Ben.

deiussum 11-24-2004 08:20 AM

I'm guessing that your problem isn't so much that your data is being saved w/o the line breaks as it is that when displayed the HTML does what HTML does and ignores those line breaks and extra whitespace when displayed.

You can fix this by replacing the line breaks with a <br> using the str_replace method. Either before storing the data in the database, or after you read the data from the database and before you display it...

Something like:

Code:

$sFormattedString = str_replace("\n", "<br>", $sOriginalString);

sigsegv 11-24-2004 05:48 PM

And don't forget to use mysql_real_escape_string()

SQL injection is a bad thing.


All times are GMT -5. The time now is 08:38 PM.