Multiple lines and preg_match

Discussion in 'PHP' started by iAreCow, Jul 24, 2010.

  1. #1
    Good day!

    I've got a small problem to solve, I couldn't get preg_match to work with multiple lines, here's the code:
    <?php
    $page = '<td class="top40txt3" align="left"><strong>Alejandro</strong></td> 
            <td class="top40txt3" align="left">Lady GaGa</td>';
    preg_match('!<td class="top40txt3" align="left"><strong>(.+?)</strong><td class="top40txt3" align="left">(.+?)</td>!sm',$page,$matches);
    print_r($matches);
    ?>
    PHP:
    I've tried all possible modifiers, but I always get zero output. Also tried quite a bit of Googling with no luck.

    Thanks for the time :)
     
    iAreCow, Jul 24, 2010 IP
  2. atxsurf

    atxsurf Peon

    Messages:
    2,394
    Likes Received:
    21
    Best Answers:
    1
    Trophy Points:
    0
    #2
    you have missed
    </td>(.+?)
    PHP:
    for td closing tag and newline + spaces/tabs
     
    atxsurf, Jul 24, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    <?php
    $page = '<td class="top40txt3" align="left"><strong>Alejandro</strong></td>
            <td class="top40txt3" align="left">Lady GaGa</td>';
    preg_match('~<td class="top40txt3" align="left"><strong>(.+?)</strong></td>\s*<td class="top40txt3" align="left">(.+?)</td>~', $page, $matches);
    print_r($matches);
    ?>
    PHP:
     
    danx10, Jul 24, 2010 IP
  4. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Thank you atxsurf and danx10, problem is actually solved!
    I'm going to investigate what I did wrong myself.
     
    iAreCow, Jul 24, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    Where-ever their was a new line; replace it with \s*
     
    danx10, Jul 24, 2010 IP
  6. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Or with \s+
    But thank you very much guys! :)
     
    iAreCow, Jul 24, 2010 IP
  7. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You could also use the s modifier so the that the dot metacharacter in the pattern matches everything, including newlines.
     
    Deacalion, Jul 24, 2010 IP
  8. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #8
    I see, that worked as well, but, I'm a little confused about the "ungreedy" thing. I had a 248 patterns to match.
    .+? with modifier s works
    .+ with modifier s does not work (only matches the first pattern)

    \s+? with no modifier works
    \s+ with no modifier works

    Ungreedy pattern consumes as few characters as possible.
    Greedy pattern consumes as many characters as possible. It is because it is "greedy".

    - Source

    So, there has to be some sort of logic inside it...
     
    iAreCow, Jul 24, 2010 IP
  9. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    The link you've given explains it pretty well. Here's my explanation:

    
    <?php
        // our string
        $string = "It's colder than a penguin's bollocks";
        
        // non greedy will match everything up to the first apostrophe
        if (preg_match("/^(.*?)\'/", $string, $match))
            echo 'Non-Greedy: ' . $match[1];
            
        echo '<br />';
        
        // greedy will match everything up to the last apostrophe
        if (preg_match("/^(.*)\'/", $string, $match))
            echo 'Greedy: ' . $match[1];
    ?>
    
    PHP:
     
    Deacalion, Jul 25, 2010 IP
  10. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #10
    These make much more sense, got it now, thanks a lot Deacalion!
     
    iAreCow, Jul 25, 2010 IP