Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
| Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To 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 June 15th, 2004, 07:39 PM
Authorized User
 
Join Date: Feb 2004
Location: Nashville, TN, USA.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help! Empty paragraph tags!

Please help - I'm an experienced ASP coder but I'm a raw beginner at PHP. I'm trying to create a function to format text as actual HTML paragraphs, rather than cheating and just inserting "<br />" tags. The function goes like this:

function CleanString($varstring) {
    $varstring = stripslashes($varstring);
    $varstring = '<p>' . $varstring . '</p>';
    $varstring = str_replace("\n", "</p><p>", $varstring);
    $varstring = str_replace("<p></p>", "", $varstring);
    return $varstring;
    }

This works great, with one exception: If there are multiple line breaks, there are multiple sets of empty paragraph tags. That's what I'm trying to correct in the last line of the function, but I'm having no luck. Any help would be greatly appreciated.
__________________
__________________________________________________ _____________

Confidence is what you feel when you do not truly understand the situation.
 
Old June 15th, 2004, 08:57 PM
Authorized User
 
Join Date: Feb 2004
Location: Nashville, TN, USA.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Found my solution! The problem was with carriage returns (who knew?):

function CleanString($varstring) {
     $varstring = stripslashes($varstring);
     $varstring = '<p>' . $varstring . '</p>';
     $varstring = str_replace("\r", "\n", $varstring); // the correction
     $varstring = str_replace("\n", "</p><p>", $varstring);
     $varstring = str_replace("<p></p>", "", $varstring);
    return $varstring;
    }

Alternately, there's a slightly more robust solution at http://www.photomatt.net/scripts/autop for anyone experiencing similar trouble.

Confidence is what you feel when you do not truly understand the situation.




Similar Threads
Thread Thread Starter Forum Replies Last Post
paragraph problem dzisaacs XSLT 2 December 31st, 2005 02:49 AM
Empty tags in resulting html? C.R. Chambers XSLT 6 July 22nd, 2005 03:59 PM
get paragraph from database vickyj Classic ASP Databases 2 May 31st, 2005 07:59 AM
empty tags vs. singletons hankshiny XSLT 1 April 9th, 2005 07:37 AM
Indented paragraph ric Beginning VB 6 1 February 26th, 2004 06:08 AM





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