Regular Expressions Again!!!!

Discussion in 'PHP' started by THT, Oct 18, 2005.

  1. #1
    Right.

    Im trying to use preg_match_all() to get some strings:

    the source file is here:

    http://www.ps3peak.com/src.txt
    Code (markup):
    and the code im using to get this is:

    $source= file_get_contents("src.txt");
    
    preg_match_all("/<span class=\"postbody\">(.*)<\/span>/mi", $cource, $posts);
    print_r($posts[1]);
    PHP:
    but i only get 3 results when i should get 4

    Ive used the "m" modifier as the source is multiline.... any clues?
     
    THT, Oct 18, 2005 IP
  2. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try adding the "U" flag to the im modifiers - it makes the expression ungreedy so that it will match the smallest chunk of text that it can rather than the largest, which is the default. It could be matching one from one <span> to the second </span> rather than the first which is what you want

    cheers

    johnt
     
    johnt, Oct 18, 2005 IP
  3. THT

    THT Peon

    Messages:
    686
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    tried that.... no joy

    its cos of the new lines as if i edit it to all be on one line it works....
     
    THT, Oct 18, 2005 IP
  4. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try:

    
    
    preg_match_all("/<span class=\"postbody\">(.*)<\/span>/simU", $source, $posts);
    
    
    Code (markup):
    Should work with your newlines.
     
    durango, Oct 18, 2005 IP
  5. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #5
    another option is to lose the newlines altogether (if you don't need them) before doing the preg_match_all
    $source = str_replace ("\n", ' ', $source);
    $source = str_replace ("\r", ' ', $source);
    PHP:
     
    exam, Oct 19, 2005 IP
  6. THT

    THT Peon

    Messages:
    686
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yeah did that in the end

    Bloody hosting copany deleted my forum so i had to rebuild it from the google cache!!
     
    THT, Oct 21, 2005 IP
  7. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #7
    >> Bloody hosting copany deleted my forum so i had to rebuild it from the google cache!!

    Hope you have a new hosting company now :D
     
    exam, Oct 21, 2005 IP