Preg_match fustratoins

Discussion in 'PHP' started by Andreajr, Dec 15, 2008.

  1. #1
    I am getting so mad can someone look at this I am trying to learn how to scrape parts off a website:

    <?php
    $data = file_get_contents('http://search.msn.com/results.aspx?q=site%3Afroogle.com');
    $regex = '/Page 1 of (.+?) /results/div id="search_header"><h1>site:froogle.com</h1><h5>Page 1 of 9,138 results</h5>&nbsp;<b>&#01/';
    preg_match($regex,$data,$match);
    var_dump($match);
    ?>

    error:

    Warning: preg_match() [function.preg-match]: Unknown modifier 'r' in idcidkidcidkidc.php on line 4
    NULL

    please help
     
    Solved! View solution.
    Andreajr, Dec 15, 2008 IP
  2. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #2
    I think you have to escape the '/' that appears before r.

    Not sure, not a big regex expert.

    Try putting a '\' in front of all of those and see what happens.
     
    qualityfirst, Dec 15, 2008 IP
  3. Andreajr

    Andreajr Peon

    Messages:
    90
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Me ethier lol as you can tell i am a n00b
     
    Andreajr, Dec 15, 2008 IP
  4. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    You are using slash as delimiters and the regex itself contains some slashes. So either escape the slashes in the regex by adding a backslash in front of them, or use a different character as delimiters (e.g.: $regex = '|Page 1 of ... &nbsp;<b>&#01|';
     
    phper, Dec 15, 2008 IP
  5. Andreajr

    Andreajr Peon

    Messages:
    90
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can someone give me an example of how to do this I can a n00b I dont know what u mean by escape the slashes. Can someone explain to a n00b how to do this?
     
    Andreajr, Dec 16, 2008 IP
  6. #6
    Lesson 1: Escaping

    What would you do if you want a newline character in a string?
    $sting = "this is a \n newline.";
    PHP:
    What if you want a double quote? Sure you can alternate it.
    $string = 'This string contains a " in it';
    PHP:
    Or you can "escape" it.
    $string = "This string contains a \" in it";
    PHP:
    In your case:
    I would just use a different deliminator
    $regex = "%Page 1 of (.+?) /results/div id=\"search_header\"><h1>site:froogle.com</h1><h5>Page 1 of 9,138 results</h5>&nbsp;<b>%";
    PHP:
    or do it the hard way.
    $regex = "/Page 1 of (.+?) \/results\/div id=\"search_header\"><h1>site:froogle\.com<\/h1><h5>Page 1 of 9,138 results<\/h5>&nbsp;<b>/";
    PHP:
     
    Kaizoku, Dec 16, 2008 IP
  7. Andreajr

    Andreajr Peon

    Messages:
    90
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank You So So So much
     
    Andreajr, Dec 16, 2008 IP