2

Hi I have an issue with the regular expression based off the question below:
Question: [php-regex-to-read-select-form][1]

The HTML code I wish for the regular expression to go through

<select name="List">
    <option selected="selected" value="HAR">HAR</option>
    <option value="HAY">HAY</option>
    <option value="ZZZ">ZZZ</option>
</select>

The php I currently have is as follows:

preg_match_all( '@(<option value="([^"]+)">([^<]+)<\/option>)@', $html, $viewstate);

    echo "<pre>";
    print_r($viewstate);
    echo "</pre>";
    die();

The output from that is as follows:

Array
(
    [0] => Array
        (
            [0] => HAY
            [1] => ZZZ
        )

    [1] => Array
        (
            [0] => HAY
            [1] => ZZZ
        )

    [2] => Array
        (
            [0] => HAY
            [1] => ZZZ
        )

    [3] => Array
        (
            [0] => HAY
            [1] => ZZZ
        )

)

So I am a bit confused why it creates the results muiltiple times and do now know how to get it to get the <option tag with the selected attribute set.

1
  • Check the manual. It's multiple times because you're using groups. But there's more to it, check the source code in the browser and you will see another output. Basically group 1 is redundant. Group 0 contains the whole <option></option>, group 2 the value attribute and group 3 the value that's in between <option></option>. Pro-tip: if you're not familiar with regex, go for an html-parser. It's much more easier and reliable
    – HamZa
    Jun 5, 2014 at 6:26

2 Answers 2

2

Your code returns the following :

Array ( 
    [0] => Array ( 
        [0] => <option value="HAY">HAY</option>
        [1] => <option value="ZZZ">ZZZ</option>
    ) 
    [1] => Array ( 
        [0] => <option value="HAY">HAY</option>
        [1] => <option value="ZZZ">ZZZ</option>
    ) 
    [2] => Array ( // all values
        [0] => HAY 
        [1] => ZZZ 
    ) 
    [3] => Array ( // all text for the option
        [0] => HAY 
        [1] => ZZZ 
    ) 
) 

So, you can use accordingly of your use. When you are printing in the webpage, it seems it is repeating the result. But in actual, it is giving right output.

Now, if you want to use the values, then you can use $viewstate[2] and for option text, you can use $viewstate[3]. Thanks and this might help you.

The same way, if you want to get the selected value($viewstate[2])/text($viewstate[3]), then you can use the following:

preg_match_all( '@(<option selected="selected" value="([^"]+)">([^<]+)<\/option>)@', $html, $viewstate);

Array ( 
    [0] => Array ( 
        [0] => <option value="HAR" selected="selected">HAR</option>
    ) 
    [1] => Array ( 
        [0] => <option value="HAR" selected="selected">HAR</option>
    ) 
    [2] => Array ( 
        [0] => HAR 
    ) 
    [3] => Array ( 
        [0] => HAR 
    ) 
) 
1

I wrote a function to generate array key value of select option list here Sample code

To get an array from list of countries combo box that I inspect html from other website.

Hope it useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.