[help]preg_match_all pattern

Discussion in 'PHP' started by Eng_A_Moktar, Jul 7, 2009.

  1. #1
    Hi, I wanna make a general pattern for example i have this string , i wanna capture the titles and links of this topic :

    <?php
    $str="<td valign='top'>
    <center>
    <a href='redirect-5321'>
    Zito, Romo combo zero
    <br><br>
    <img src='images/img280745.png' border='0' width='190' height='190'></a>
    </center>
    <br>
    </td></tr><tr>
    <td valign='top'>
    <center>
    <a href='redirect-5322'>
    NBA salary cap set at $57.7 million
    <br><br>
    <img src='images/img280746.png' border='0' width='190' height='190'></a>
    </center>
    <br>
    </td></tr><tr>";

    // define start and end
    $title_start = ">"; $title_end = "<br><br>";
    $link_start = "<a href='"; $link_end = "'";

    $code = preg_match_all("/{$title_start}(.*?){$title_end}/", $str, $result1);
    $code = preg_match_all("/{$link_start}(.*?){$link_end}/", $str, $result2, PREG_PATTERN_ORDER);


    echo $result1[1][0];
    echo "<br />";
    echo $result2[1][0];
    echo "<br />";
    ?>

    in this example, title pattern doesn't work, because there are spaces and lines,
    what i wanna say, how can i make my pattern wider and flexible, even works with tags or words ?
    thanks.
     
    Eng_A_Moktar, Jul 7, 2009 IP
  2. DimitryKislichenko

    DimitryKislichenko Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi Eng_A_Moktar,

    Try this pattern <a\s+href='(.*?)'>(.*?)<br>

    Example:
    preg_match_all("|<a\s+href='(.*?)'>(.*?)<br>|isU", $str, $result1);
    Code (markup):
     
    DimitryKislichenko, Jul 8, 2009 IP