Page 1 of 1

Storing formatted text in mysql Db for echo statement

Posted: Thu Nov 13, 2008 4:46 pm
by jporven
I am new at using PHP and MySQL databases, so kindly bear with me if my questions are overtly simple. I greatly appreciate any help with the following, as forum searches and online references have not given me a direct answer.

I am trying to store a formatted text paragraph in a MySQL database longtext field. I would like to use the fields value in an echo statement to display the paragraph on a web page.

Despite multiple different approaches, my stored text is never displayed with the formatting intact. I assume this problem has two parts. First the formatting of the text I am pasting into my longtext field (I am using navicat to visually edit the mysql tables/records). Secondly, my php code to dynamically display the lontext field's value may be incorrect.

To address my first question:
I have tried pasting text directly from MS word and also from notepad, as well as html formatted text from Adobe Dreamweaver. None has worked. Can someone confirm the correct text format I must use when storing text using longtext (plain text, etc.) and also whether long text is the correct field type to use? (I will be storing long essays).

To address my second question, I have tried reformatting the field's value upon display using the following code snippets:

Code: Select all

 
<?php echo $row_Recordset1['Bio']; ?>
    
<?php print nl2br(str_replace(" ","&nbsp;",stripslashes($row_Recordset1['Bio']))); ?>
 
<?php 
        $text= htmlspecialchars(htmlentities ($row_Recordset1['Bio']));
        echo $text;
?>
     
<?php $text2 = $row_Recordset1['Bio'];
          $text2 = str_replace("\n", "<br>", $text2);
           echo $text2;
?>
 
None of the above have worked, does anyone know of a way to display stored text while keeping some formatting? I would prefer to be able to store the text with some HTML formatting and line/parapgraph breaks, but will settle for just line/paragraph breaks if this is not possible or simple.

Re: Storing formatted text in mysql Db for echo statement

Posted: Thu Nov 13, 2008 5:34 pm
by requinix
jporven wrote:I have tried pasting text directly from MS word and also from notepad, as well as html formatted text from Adobe Dreamweaver. None has worked.
Text is text. What makes it look special is the metadata associated with it. Word has one way of doing it, HTML is another way.
If you want to preserve formatting you need to keep that metadata. There's no universal way to display Word info so you need to have it in HTML format.
This text has some italics and underlines. If you copy and paste it into Notepad and then into a web page you won't see any formatting.
This text has some <span style="font-style: italic">italics</span> and <span style="text-decoration: underline">underlines</span>. If you copy and paste it into Notepad and then into a web page you WILL see the formatting.