Page 1 of 1

Convert <P> tags to <BR>

Posted: Fri Apr 28, 2006 1:39 am
by furiousweebee
I've always really hated paragraph tags (I prefer to use BR's) and have just implemented a rich HTML editor into my site's custom CMS. The only problem is, the editor can only accept P tags by default. So, once I've submitted that and it's in my database, this is how an entry looks, once extracted:

Code: Select all

<span class="article_body"><p>How well does this work?</p><p>&nbsp;</p><p>This should be two lines <strong>dow</strong>n.</p><p>&nbsp;</p><p>&nbsp;</p><p>This should be three lines down!&nbsp;</p></span>
<a href="index.php?view=news&id=5"><img src="images/read_more.gif" alt="read the full article" border="0"></a>
I want to remove all of the P tags, replacing them with <br> tags, and I was going to use the following code to do it:

Code: Select all

$bad = array("<p>", "</p>", "&nbsp;");
$good  = array("", "<br /><br />", "");
$_POST['article'] = str_replace($bad, $good, $_POST['article']);
However, that means that the final part of each entry will be two line breaks (whereas I don't want any since I'm pulling out multiple entries at once). So, my question is: How would I make a simple way of ensuring that all paragraphs are converted to line breaks, but the very last replace shouldn't occur?

I hope that's not too confusing...

Posted: Fri Apr 28, 2006 1:46 am
by feyd
regex may be your best bet, however using some clever strpos() or similar calls could do it too.

Posted: Fri Apr 28, 2006 1:51 am
by furiousweebee
Do you think this would work?

Code: Select all

$string = ereg_replace("<br />", "$", $string);
I'm not an ace with PHP or anything... but would that remove all <br /> tags from the end of the page?

Posted: Fri Apr 28, 2006 2:16 am
by feyd
try it.

Posted: Fri Apr 28, 2006 2:17 am
by furiousweebee
I had a feeling you'd say that lol.

Re: Convert <P> tags to <BR>

Posted: Fri Apr 28, 2006 2:31 am
by furiousweebee
Ok, I've got it from:

Code: Select all

<span class="article_body"><p>How well does this work?</p><p>&nbsp;</p><p>This should be two lines down.</p><p>&nbsp;</p><p>&nbsp;</p><p>This should be three lines down!&nbsp;</p>
to:

Code: Select all

<span class="article_body">How well does this work?<br /><br />This should be two lines down.<br /><br /><br />This should be three lines down! <br /><br /><br /><br /></span>
This is my PHP:

Code: Select all

// remove paragraphs from article
$bad = array("<p>", "</p>", "&nbsp;");
$good  = array("", "<br />", "");
$_POST['article'] = str_replace($bad, $good, $_POST['article']);
$_POST['article'] = nl2br($_POST['article']);
$_POST['article'] = ereg_replace("\$", "<br />", $_POST['article']);
So now it works to the point where I'm just left with those extra <br /> tags at the end. Essentially I just need to strip them away and it's all good. I'm not sure if this could be more efficient somehow, but if I can just get this sorted out then I can finish my site off so any further help would be great.

Posted: Fri Apr 28, 2006 2:33 am
by feyd
read through the regex board's stickies and play with it. You'll probably figure it out soon enough.