?
Solved

Insert formatted text in mysql database

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

809 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