Pattern Matching

Discussion in 'PHP' started by projectWORD, Jul 20, 2008.

  1. #1
    Hi Everyone,
    What is the best way to extract X Y and Z from the following.

    <td width="285" valign="top" bgcolor="#FFFFFF"><font class="normal">X<br>Y<br>Z</font></td>

    Thanks
     
    projectWORD, Jul 20, 2008 IP
  2. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    $in = "<td width=\"285\" valign=\"top\" bgcolor=\"#FFFFFF\"><font class=\"normal\">Heres<br>Your<br>Codes</font></td>";
    preg_match("/>(.*)<br>(.*)<br>(.*)<\/font><\/td>/",$in,$out);
    echo $out[1] . "<br />";
    echo $out[2] . "<br />";
    echo $out[3] . "<br />";
    ?>

    There ya go mate
     
    cornetofreak, Jul 20, 2008 IP
  3. projectWORD

    projectWORD Active Member

    Messages:
    287
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #3
    Thanks, Ill try that now. I thought preg_match would be the way just couldn't think of the syntax. Thanks again.
     
    projectWORD, Jul 20, 2008 IP
  4. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    np

    say if it works cuz it gets in google within minutes so other ppl can see

    $rep++;
     
    cornetofreak, Jul 20, 2008 IP
  5. projectWORD

    projectWORD Active Member

    Messages:
    287
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #5
    preg_match("<font class='normal'>(.*)<br>",$text,$out);

    What about this to get just X.

    Warning: preg_match(): Unknown modifier '('
     
    projectWORD, Jul 20, 2008 IP
  6. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hey dood if you just want X from the string you are doing just use the one i sent you and echo X out with

    echo $out[1] . "<br />";

    ^^ this is wahatever x is you dont need to creat a new code
     
    cornetofreak, Jul 21, 2008 IP
  7. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    also wiht preg and match you have to state where you regular expression starts and where it ends , there are several ways to do this but i like the forwerd slash //


    preg_match("<font class='normal'>(.*)<br>",$text,$out); << URS
    preg_match("/<font class=\'normal\'>(.*)<br>/is",$text,$out); << MINE

    the is after the last / slash means > i < for case insencitive and > s < egnore whitespace
    the baclslash tells the script the your are looking for a literla charactor

    search for regex cheet sheet for more help with the modifiers

    Hope this helps you get started mate
     
    cornetofreak, Jul 21, 2008 IP