Solved

Insert formatted text in mysql database

Posted on 2004-09-26
3
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 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