Please give me some help with preg_replace regex

Discussion in 'PHP' started by adresaklumea, May 2, 2009.

  1. #1
    Please give me some help with preg_replace regex
    I have this html
    
    <a title="Timberland Earthkeepers tv commercial" href="http://www.adsneeze.com/clothing-footwear/timberland-earthkeepers-tv-commercial">
    <img width="300" src="http://www.adsneeze.com/files/media-front/Timberland-Earthkeepers-ad.jpg"/>
    </a>
    The main idea of advertisement is that if you help the nature, the nature ... 
    more ->
    <a title="Timberland Earthkeepers tv commercial" href="http://www.adsneeze.com/clothing-footwear/timberland-earthkeepers-tv-commercial">Timberland Earthkeepers tv commercial</a>
    
    <div class="feedflare">
    <a href="http://feeds2.feedburner.com/~ff/AdSneeze-Ads?a=CXhlKtszp50:4hHyLndkL4w:yIl2AUoC8zA">
    <img border="0" src="http://feeds2.feedburner.com/~ff/AdSneeze-Ads?d=yIl2AUoC8zA"/>
    </a>
    <a href="http://feeds2.feedburner.com/~ff/AdSneeze-Ads?a=CXhlKtszp50:4hHyLndkL4w:F7zBnMyn0Lo">
    <img border="0" src="http://feeds2.feedburner.com/~ff/AdSneeze-Ads?i=CXhlKtszp50:4hHyLndkL4w:F7zBnMyn0Lo"/>
    </a>
    </div>
    
    <p>
    <a href="http://feedads.g.doubleclick.net/~a/DoZCMoSsD67yAdsBepwwwwfo5M/0/da">
    <img border="0" ismap="true" src="http://feedads.g.doubleclick.net/~a/DoZCMwwwwsBep7d4ITfo5M/0/di"/>
    </a>
    <br/>
    <a href="http://feedads.g.doubleclick.net/~a/DoZCMowwww7d4ITfo5M/1/da">
    <img border="0" ismap="true" src="http://feedads.g.doubleclick.net/~a/DoZqwqwqAdsBep7d4ITfo5M/1/di"/>
    </a>
    </p>
    
    <img height="1" width="1" src="http://feeds2.feedburner.com/~r/AdSneeze-Ads/~4/CXhlKtszp50"/>
    
    
    Code (markup):
    I want to remove
    
    <a href="http://feeds2.feedburner.com/~ff/AdSneeze-Ads?a=CXhlKtszp50:4hHyLndkL4w:yIl2AUoC8zA">
    <img border="0" src="http://feeds2.feedburner.com/~ff/AdSneeze-Ads?d=yIl2AUoC8zA"/>
    </a>
    <a href="http://feeds2.feedburner.com/~ff/AdSneeze-Ads?a=CXhlKtszp50:4hHyLndkL4w:F7zBnMyn0Lo">
    <img border="0" src="http://feeds2.feedburner.com/~ff/AdSneeze-Ads?i=CXhlKtszp50:4hHyLndkL4w:F7zBnMyn0Lo"/>
    </a>
    
    Code (markup):
    and
    
    <a href="http://feedads.g.doubleclick.net/~a/DoZCMoSsD67yAdsBepwwwwfo5M/0/da">
    <img border="0" ismap="true" src="http://feedads.g.doubleclick.net/~a/DoZCMwwwwsBep7d4ITfo5M/0/di"/>
    </a>
    <a href="http://feedads.g.doubleclick.net/~a/DoZCMowwww7d4ITfo5M/1/da">
    <img border="0" ismap="true" src="http://feedads.g.doubleclick.net/~a/DoZqwqwqAdsBep7d4ITfo5M/1/di"/>
    </a>
    
    Code (markup):
    .

    Please help.
     
    adresaklumea, May 2, 2009 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    Something like this should work:

    .*(<div class="feedflare">.*<\/div><p>.*<\/p>).*

    Just take out everything between the div and p tags.

    Peace,
     
    Barti1987, May 2, 2009 IP
  3. dutterback

    dutterback Banned

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Where is the best place to learn about the parameters that go into preg_replace. I have looked at the phpmanual site, and it explains the function, but not about the parameters and how it works. Thanks.
     
    dutterback, May 3, 2009 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Search on Google for "regexp" (regular expressions) - that is what is being used.
     
    PoPSiCLe, May 3, 2009 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    1. . -> represents ANY one character
    2. [a-z] -> represents ANY characters from a to z, can also be in the form [A-Z],[0-9],[a-z -_]
    3. .* -> will match 0 or more occurrences of any character, can also use [a-z]* etc..
    4. .+ -> will match 1 or more occurrences of any characters, can also use [0-9]+ etc..
    5. [a-z]{2} -> will match only 2 occurrences of characters from a to z
    6. [a-z]{2,3} -> will match only 2 to 3 occurrences of characters from a to z
    7. ^[a-z][0-9]+ -> ^ acts as an anchor, the beginning must be a one letter from a to z, then any number >1 of numbers.
    8. $\.php -> Acts like $, but at the end of a match - in this ex, it will return all php extensions
    \ -> an escape string, like the $ examples, the following must be escaped: . + * ^ $ {} () if you want to use them as characters and not parameters, to use the \ string, it will also need to be escaped like: \\

    Add a parameter in () and it will return that match for you, ex.: ([0-9]{4}), will return any numbers of size 4.

    The above is true for most languages beside PHP.

    For PHP, when using a regular expression, they need to be in this format:

    'CHARACTER EXPRESSION CHARACTER MODIFIER'
    • Where CHARACTER is something like /| etc.., they just need to match: /[a-z]/ or |[a-z]|
    • EXPRESSION is the regular express itself, [a-z]+
    • MODIFIER is a modifier to tell the parser what and how to do, examples are here:
    http://www.phpbuilder.com/manual/en/reference.pcre.pattern.modifiers.php

    Peace,
     
    Barti1987, May 4, 2009 IP