Help with regex

Discussion in 'PHP' started by xenon2010, Jan 10, 2010.

  1. #1
    hi
    I have this html text.

    <select name=crap>
    <option value="0" selected="selected"></option>
    <!-- random comment -->
    <option value="1">some text</option> 
    
    <select name=crap2>
    <option value="0" selected="selected"></option>
    <!-- random comment -->
    <option value="3">some text2</option> 
    HTML:
    I need to get the value of the second option for both selectboxes.
    the values should be 1 and 3.

    so here is what've done till now with my regex pattern:
    $pat = '/<select[^>]*name\=\"[^\"]*\"[^>]*>[^<]*<option[^>]*>[^<]*<\/option>[^<]*<option[^>]*value\=\"([^\"]*)\"[^>]*>/s';
    PHP:
    the problem is this pattern works only with the first option for both selectboxes I dont know how to ignore the <!-- random comment --> part..
    I tried to put [^(<option)]* instead of [^<]* but no luck of makin this thing work..

    any help would be appreciated..
     
    xenon2010, Jan 10, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Here you go, this should retrieve value 1 and value 3:

    <?php
    
    $html = <<<HTML
    <select name=crap>
    <option value="0" selected="selected"></option>
    <!-- random comment -->
    <option value="1">some text</option>
    
    <select name=crap2>
    <option value="0" selected="selected"></option>
    <!-- random comment -->
    <option value="3">some text2</option>
    HTML;
    
    
    if(preg_match_all('#<option value="([^\"]*)">#s', $html, $matches)) {
    
    //value 1
    $firstvalue = $matches[1][0];
    
    //value 3
    $secondvalue = $matches[1][1];
    
    } else {
    
    print("No matches found");
    
    }
    
    ?>
    PHP:
     
    danx10, Jan 10, 2010 IP
  3. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks but this is not I'm lookin for..
    I have more than 2 select boxes with lots of options..
    what I need is to get the value of second option for each select box in the page..
     
    xenon2010, Jan 10, 2010 IP
  4. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    the question is how can I ignore whole word using carrot ^.
    for example I want to use it like this: [^(<option)] I want to ignore the string in ()
    but this is not workin.
     
    xenon2010, Jan 10, 2010 IP