Solved

Insert formatted text in mysql database

Posted on 2004-09-26
3
240 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
Comment Utility
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
Comment Utility
It works great, thanx!
0
 
LVL 27

Expert Comment

by:Diablo84
Comment Utility
no problem :)
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
htacces issue 2 37
posting form data 3 24
How to validate 2 forms at once 8 25
compressing images after upload 4 28
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

772 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