Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    342
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Syntax in preg_replace problem

    Greetings,

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

    PHP Code:
    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!
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Escape the delimiter in the expression or use a different delimiter.

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

  • Users who have thanked MattF for this post:

    MrBiggZ (05-31-2011)

  • #3
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    342
    Thanks
    44
    Thanked 2 Times in 2 Posts
    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:

    PHP Code:
    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.

    Code:
    					<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:
    PHP Code:
            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>"
    ;}
            } 
    Last edited by MrBiggZ; 05-31-2011 at 04:39 AM.
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by MrBiggZ View Post
    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.

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by MrBiggZ View Post
    I just found out that those empty p tags aren't in all together. Is this valid:

    PHP Code:
    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:


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

  • Users who have thanked MattF for this post:

    MrBiggZ (05-31-2011)

  • #6
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    342
    Thanks
    44
    Thanked 2 Times in 2 Posts
    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:

    PHP Code:
    preg_replace('~&lt;p&gt;(\&amp;nbsp;)?&lt;/p&gt;~','',$item['description']); 
    Works like a charm!
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by MrBiggZ View Post
    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. So annoying when the penny finally drops hours later though, ain't it.


    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •