preg_match_all help

Discussion in 'PHP' started by crazyryan, Apr 3, 2008.

  1. #1
    Hey

    I have this as my preg_match_all line:
    
    preg_match_all('<a name="\/(.*)\/s"><\/a><div class="coupon" id="\/(.*)\/s"><table class="details"><tr><th class="code">Code:<\/th><td class="code" id="\/(.*)\/s"><strong>\/(.*)\/s<\/strong> <a class="useButton" href="\/out\/\/(.*)\/s" target="_blank" onclick="javascript:pageTracker._trackPageview(\'\/outgoing\/naff\/\/(.*)\/s\');" rel="nofollow"><span>Use coupon &raquo;<\/span><\/a><div class="affTracker"><\/div><\/td><\/tr><tr><th>Discount:<\/th><td class="discount">\/(.*)\/s</td><\/tr><tr><th>Stats:<\/th><td class="stats"><span class="average">\/(.*)\/s</span> <img src="\/(.*)\/s" alt="Coupon voting trend" /><\/td><\/tr><\/table><!-- END: .details -->', $page, $output, PREG_PATTERN_ORDER);
    PHP:
    On page I have:
    
    <a name="551321"></a><div class="coupon" id="account551321"><table class="details"><tr><th class="code">Code:</th><td class="code" id="code551321"><strong>BIGBRAND</strong> <a class="useButton" href="/out/551321" target="_blank" onclick="javascript:pageTracker._trackPageview('/outgoing/naff/amazon.com');" rel="nofollow"><span>Use coupon &raquo;</span></a><div class="affTracker"></div></td></tr><tr><th>Discount:</th><td class="discount">Get $25 off $59 certain food brands at Amazon.com exp 3/31</td></tr><tr><th>Stats:</th><td class="stats"><span class="average">45% success rate</span> <img src="http://assets.retailmenot.com/charts/551/321.png" alt="Coupon voting trend" /></td></tr></table><!-- END: .details -->
    Code (markup):
    Repeated several times with different info, I thought the code I had above would work but it's giving me an error..


    Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '<' in /home/ryan/public_html/test.php on line 4
     
    crazyryan, Apr 3, 2008 IP
  2. mulari

    mulari Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The specific error is because of the <img /> tag. you need to change /> to \/>.

    I also think your \/(.*)\/s should be changed (.*?), for example:
    <img src="(.*?)" alt="Coupon voting trend" \/>

    Hope this 2 will do the trick
     
    mulari, Apr 3, 2008 IP
  3. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #3
    Still can't get it working ... heres my final code:
    <?php
    if(isset($_GET['domain'])) {
    	$page = file_get_contents("http://www.retailmenot.com/view/" . $_GET['domain'] . "");
    	preg_match_all('<a name="(.*?)"><\/a><div class="coupon" id="(.*?)"><table class="details"><tr><th class="code">Code:<\/th><td class="code" id="(.*?)"><strong>(.*?)<\/strong> <a class="useButton" href="\/out\/(.*?)" target="_blank" onclick="javascript:pageTracker._trackPageview(\'\/outgoing\/naff\/(.*?)\');" rel="nofollow"><span>Use coupon &raquo;<\/span><\/a><div class="affTracker"><\/div><\/td><\/tr><tr><th>Discount:<\/th><td class="discount">(.*?)</td><\/tr><tr><th>Stats:<\/th><td class="stats"><span class="average">(.*?)</span> <img src="(.*?)" alt="Coupon voting trend" \/><\/td><\/tr><\/table><!-- END: .details -->#', $page, $output, PREG_PATTERN_ORDER);
    	print_r($output);
    	} else {
    }
    ?>
    PHP:
     
    crazyryan, Apr 3, 2008 IP
  4. mulari

    mulari Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Perhaps the regex is not correct and need some correcting, you should try shortening it a little and trying adding piece by piece, start with:
    See if it return results, then add the rest of the regexp, one tag at a time, i'm pretty sure you'll find that you missed something.

    Thats the "problem" with preg_match, it's all or nothing, no easy way to know whats wrong.
     
    mulari, Apr 3, 2008 IP
  5. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #5
    Heh, even that gives the unknown modifer error. :p

    EDIT: What exactly does adding # before and after the regex, because that prints an array with values...
     
    crazyryan, Apr 3, 2008 IP
  6. mulari

    mulari Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oops :)
    Yea, in the help they show a regexp should be surrounded with / (or maybe some use #), from the PHP help:
    $pattern = '/^def/';

    Not sure exactly what is the origin of it and always thought it's not mandatory, but perhaps it is.
     
    mulari, Apr 3, 2008 IP