English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm creating a blog site in PHP. Users can enter a title for their blog in a textbox, and the body of their blog in a textarea box.

I'm storing the body of their blog in a PHP database as a varchar type.

When I retrieve the body of a blog from the database and output it to the screen, will the blog lose all formatting (e.g. paragraph breaks, indents, etc.)? If so, how can I preserve the formatting?

2007-02-05 05:57:48 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

You can use a series of str_replace to do what you want.

Assume that the variable containing the text you want to format is named $body.

To replace line breaks with HTML line breaks:

$body = str_replace("\n", "
", $body);

To replace tabs with non-breaking spaces, just use the non-breaking space entity (which Yahoo won't reproduce properly; it's & nbsp; without the space, represented below with just nbsp;

$body = str_replace("\t", "nbsp;nbsp;nbsp;nbsp;", $body);

In all honesty, rather than reprogramming the wheel, I'd suggest using WordPress or Blogger.

http://us3.php.net/manual/en/function.str-replace.php

2007-02-05 07:16:27 · answer #1 · answered by Anonymous · 0 0

If you want to save formatted text, then you can enter your data from a web editor and save your data in HTML format in a filed of type TEXT.

This will cause issues when searching text. So I suggest you have two fields in your table. Into one you insert HTML formatted text, and in the other you save raw text. You use the one with raw text for search purposes.

Thank You

2007-02-05 07:36:54 · answer #2 · answered by Smutty 6 · 0 0

Try storing the content as text or tinytext. Varchar does not notice paragraphs.

If you need more help, contact me at http://www.yuchniuk.com

2007-02-05 06:07:45 · answer #3 · answered by Yuchniuk Website Design 3 · 0 0

You can include a rich text editor into your application, so that users can create their own HTML. Take a look at a few of those:

http://tinymce.moxiecode.com/
http://xinha.python-hosting.com/
http://koivi.com/WYSIWYG-Editor/

Alternatively, you can use nl2br() function to replace line breaks with
tags:

http://www.php.net/nl2br

__________

2007-02-07 04:23:17 · answer #4 · answered by NC 7 · 0 0

Put it in a

 tag.
	

2007-02-05 06:06:57 · answer #5 · answered by GodBuster 5 · 0 0

fedest.com, questions and answers