Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 19th, 2004, 01:52 AM
Authorized User
 
Join Date: Aug 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to spraveens
Default Text Format Preservation in DB

Hi
    I am entering Large amounts of text into a text area of a form which gets added to the Database(mysql), Now I want the formatting of the text(paragraphs) to also be saved so that while viewing the text I see it exactly as It was entered.How do I do it?

Thanks

Praveen.;)

 
Old March 19th, 2004, 12:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, if it's a textarea, it _is_ being preserved (as far as the etxt itself is concerned). Output it into another textarea if you don't believe me ;). What you want is to maintain the spacing and newlines when output as part of the general HTML flow, so use egregi_replace, to push your text string through it and replace all spaces with &nbsp;'s and all newlines with "<br />"s. You can detect newlines by saying:

$outPutString = eregi_replace("\n", "<br />", $your_string);

Then output $outPutString, instead of your actual string from the database.

Take a look at http://uk.php.net/manual/en/function.eregi-replace.php
 
Old March 23rd, 2004, 09:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

More comments:
  I suggest you look at using preg_replace() instead of the ereg version.
  Use nl2br() instead of a regular expression to add <br /> tags to newlines.




Take care,

Nik
http://www.bigaction.org/
 
Old March 25th, 2004, 08:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, true. I was in a hurry when I replied earlier. Here's a function I built for reformatting richtext data from textareas:

     function rich_text_format($text_field)
     {
        /*So, we've been passed a bunch of text, full of newlines, and stuff. There's a good chance
        that there are great long reams of text, among all this, that run and run. These will simply
        output on one line unless we do something to impose some sort of wrapping to them. So let's
        impose some word-wrapping, first of all. We use newlines, rather than <br />s at this stage,
        since all newlines will get turned into <br />'s, later on, by preg_replace.*/
        $text_field = wordwrap($text_field, 75, "\n");
        /*Next, let's encode any special characters such as &amp; and so on. We specify that we want
        single quotes encoding, too*/
        $text_field = htmlspecialchars($text_field, ENT_QUOTES);
        /*Now, we build an array of control characters and other stuff we want reformatting...*/
        $patterns = array("/ /", "/\n/", "/\t/");
        /*...and an array of what we want them to be replaced with.*/
        $replacements = array("&nbsp;", "<br />", "&nbsp;&nbsp;&nbsp;&nbsp;");
        /*(Add new values to each array as you see fit... Just make sure they correspond with each other and
        refer to the manual for guidance (http://uk2.php.net/manual/en/function.preg-replace.php at time of writing)

        Then do the replacement*/
        $text_field = preg_replace($patterns, $replacements, $text_field);
        //And send this thoroughly 'munged' piece of text back to where it came from...
        return $text_field;
 
Old March 30th, 2004, 01:13 PM
Authorized User
 
Join Date: Feb 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mekh Send a message via Yahoo to mekh
Default

Well, by replacing \t by &nbsp;&nbsp;&nbsp;... will replace the data from DB to next column in our spreadsheet?

        $patterns = array("/ /", "/\n/", "/\t/");
        /*...and an array of what we want them to be replaced with.*/
        $replacements = array("&nbsp;", "<br />", "&nbsp;&nbsp;&nbsp;&nbsp;");

Thanks
Take care
Mekh

mekh dev gurung
 
Old March 30th, 2004, 05:25 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, you're mixing apples and oranges. If you're formatting text to be displayed in a web browser, you need to fudge things using non-breaking spaces or whatever. If you're formatting text for CSV output, then obviously your HTML hacks won't be appropriate.

That's the beauty of PHP -- you can take the same source data and use PHP to generate the output in whatever format you want.


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Rich Text Format to Ordinary Format in ASP Andraw Classic ASP Basics 1 August 31st, 2007 10:05 AM
From DB back to Jpg Format reyboy VB.NET 2002/2003 Basics 3 April 20th, 2005 05:32 AM
to change the text format vubinhsg BOOK: Access 2003 VBA Programmer's Reference 1 December 30th, 2004 09:46 AM
Is the book available in text format sawyer4444 BOOK: Access 2003 VBA Programmer's Reference 7 September 22nd, 2004 07:53 AM
how to save text with format?? 6cet6 ASP.NET 1.0 and 1.1 Basics 4 November 20th, 2003 06:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.