Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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.
Reply With Quote
  #2 (permalink)  
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:06 PM.


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