...

View Full Version : Syntax in preg_replace problem



MrBiggZ
05-31-2011, 02:08 AM
Greetings,

I perhaps have been looking at this too long and have walled myself to the solution. I have this statement:


preg_replace('/<p>&nbsp;</p><p></p>/','',$item['description']);

and it'll tell me Warning: preg_replace() [function.preg-replace]: Unknown modifier 'p' in some line.

I though perhaps if I wrapped it in {} after the first / and before the last / I'd still get the same results. Just trying to get rid of those empty p tags.

The help is sincerely appreciated! :thumbsup:

MattF
05-31-2011, 03:26 AM
Escape the delimiter in the expression or use a different delimiter.



preg_replace('~<p>&nbsp;</p><p></p>~','',$item['description']);

MrBiggZ
05-31-2011, 04:22 AM
Hey Matt long time!

A tilda? Interesting. After you posted that I went an looked to see and from what I read that you're using the ~ as a delimiter in this case. I'm guessing that using that lets you use the / in the pattern then. Correct?

I just found out that those empty p tags aren't in all together. Is this valid:


preg_replace('~<p>&nbsp;</p>~'|'~<p></p>~','',$item['description']);

So if it finds either the one with the space in it OR the with out to replace it with null. I think?

I tried it .. It doesn't replace them. I pasted in sample of the HTML I'm working with.


<div class="newspubdate"><p>Sunday May 29, 2011 @ 4:59 pm</p></div>
<div class="newsstory"><p><img src="http://photos.handlersspot.net/albums/userpics/10001/thumb_DSCF0026.JPG" alt="DSCF0026.JPG" /></p><p>&nbsp;</p><p></p></div>

</div><div class="photo_article">
<div class="newstitle"><a href="http://photos.handlersspot.net/displayimage.php?pid=38"><p>DSCF0025.JPG</p></a></div>

<div class="newspubdate"><p>Sunday May 29, 2011 @ 4:59 pm</p></div>
<div class="newsstory"><p><img src="http://photos.handlersspot.net/albums/userpics/10001/thumb_DSCF0025.JPG" alt="DSCF0025.JPG" /></p><p>&nbsp;</p><p></p></div>

My script looks like this:

foreach($rs['items'] as $item)
{
// echo "\t<li><div class=\"newstitle\"><a href=\"$item[link]\">".$item['title']."</a></div><br /><div class=\"newspubdate\">".$item['pubDate']."</div><br /><div class=\"newsstory\">".html_entity_decode($item['description'])."</div></li>\n";
if ($fmtType == "photo") {
echo "<div class=\"photo_article\">\n";
$newDesc = preg_replace('~<p>&nbsp;</p>~'|'~<p></p>}~','',$item['description']);
}
else {echo "<div class=\"article\">\n";}

echo "<div class=\"newstitle\"><a href=\"$item[link]\"><p>".$item['title']."</p></a></div>\n
<div class=\"newspubdate\"><p>".$item['pubDate']."</p></div>\n";

if ($fmtType == "photo") {
echo "<div class=\"newsstory\"><p>".html_entity_decode($newDesc)."</p></div>\n
</div>";}
else {
echo "<div class=\"newsstory\"><p>".html_entity_decode($item['description'])."</p></div>\n
</div>";}
}

Inigoesdr
05-31-2011, 04:29 AM
I'm guessing that using that lets you use the / in the pattern then. Correct?

Yes. You can use any character you want for the delimiter.

MattF
05-31-2011, 05:15 AM
I just found out that those empty p tags aren't in all together. Is this valid:


preg_replace('~<p>&nbsp;</p>~'|'~<p></p>~','',$item['description']);

So if it finds either the one with the space in it OR the with out to replace it with null. I think?

Nope. Try this:



preg_replace('~<p>(\&nbsp;)?</p>~','',$item['description']);

MrBiggZ
05-31-2011, 06:02 AM
I think my first problem was I should have looked at the RAW code! *smack*

&lt; &gt; &amp;

It bit me!

But the (\&nbsp)? is the conditional I take it? I see this type of code to often coupled with I don't thing I've ever had the need to had a condition doing a replace. But once I changed all those <> to this:


preg_replace('~&lt;p&gt;(\&amp;nbsp;)?&lt;/p&gt;~','',$item['description']);

Works like a charm! :thumbsup:

MattF
05-31-2011, 02:50 PM
I think my first problem was I should have looked at the RAW code! *smack*

I'll bet we've all been caught out by similar at some point. :D So annoying when the penny finally drops hours later though, ain't it. :D



But the (\&nbsp)? is the conditional I take it? I see this type of code to often coupled with I don't thing I've ever had the need to had a condition doing a replace.

Aye. What that does is to make the contents of the parantheses optional. If the ? follows a string, it just makes the single character preceding ? optional. If a set of characters are enclosed within brackets, []?, it makes any one of those characters optional, whereas if you use the parantheses, ()?, it makes the complete string within those, as is, optional. That's if memory serves me correctly, btw. It's been several months since last I coded anything, so I may have erred or omitted something there. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum