?
Solved

Insert formatted text in mysql database

Posted on 2004-09-26
3
Medium Priority
?
261 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 2000 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

771 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