using RegEx to get the values of an option list

Discussion in 'PHP' started by xpotential, Feb 7, 2008.

  1. #1
    I've been banging my head on this one for a while...did a few searches on the web and here at DP forums, but I can't figure out what I'm doing wrong (aside from attempting this with very little knowledge of regex and php). :p

    I have a CURL-ed page. There's an option list that I want to parse. Here's a section of code;
    <option  value="employee1.lastname">Superman
    
    <option  value="firstname.mylastname">Batman
    Code (markup):
    I thought something like this would do it:
    preg_match_all ("/option  value=\"[^']*?\"/", $data, $matches);
    foreach ($matches[0] as $match) {
    echo $match."<br>";
    }
    
    Code (markup):
    But my results end up as: :eek:
    option value="employee1.lastname"
    option value="firstname.mylastname"

    I only want to extra the contents BETWEEN the two double quotes.
     
    xpotential, Feb 7, 2008 IP
  2. daman371

    daman371 Peon

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try taking out option and value then just trim or you could trim off option value=".
     
    daman371, Feb 7, 2008 IP
  3. xpotential

    xpotential Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ooh...thanks. A little more Google-ing got me the syntax for ltrim.

    And hex codes (/x22 /x27) for single and double quotes. (escaping the double quotes wasn't working).

    Dunno if this is elegant or not, but it works for <coding idiot>me</coding idiot>

    
    foreach ($matches[0] as $match) {
    $match = ltrim($match,"option values=");
    $match = trim($match,"\x22\x27");
    echo $match."<br>";
    
    Code (markup):
    results are now:
    employee1.lastname
    firstname.mylastname
     
    xpotential, Feb 7, 2008 IP