Solved

Insert formatted text in mysql database

Posted on 2004-09-26
3
242 Views
Last Modified: 2013-12-12
Hi, I'm building a website, and some of the content is News, when I create news, I insert in a form the Title, and the Body text. The body text is formatted with spaces, tabulations, new lines, but when I insert it in the database as a String in a mysql Text field and later request it for showing in the website, it loses all the format, spaces, new lines, etc.

How can I solve that?
0
Comment
Question by:cuesta
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 12154410
There are various functions you can use when you pull the data out of the database, for example supposing you have extracted the news data from the table and it is stored in a variable called $var...

The following converts new lines (\n) to html line breaks (<br>)

$var = nl2br($var);

You could also replace the tab character with several spaces to maintain the effect:

$var = str_replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;",$var);

If your data contains a lot of formatting it may be easier to maintain its layout just using the html tag, pre, for example:

<pre>
echo $var;
</pre>
0
 

Author Comment

by:cuesta
ID: 12154521
It works great, thanx!
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12154540
no problem :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now