tags:

views:

400

answers:

2

I would like to scan a specific select tag for all the option-tags values and content in PHP. So that from this

<select>
<option value="donald">duck</option>
<option value="david">moose</option>
</select>
<select id="something"> <!--ignore this one -->
<option value="sdfas">fs</option> <!-- ignore this one -->
...

I would get something like this:

$result['value'][0] == "donald"
$result['content'][1] == "moose"

And for that I tried:

<select>(?:[\s]*<option value="(?P<value>[^"]*)">
(?P<content>[^<]*)</option>)*[\s]*</select>

But I only get a single row (the last one) with the following code:

preg_match_all('%<select>(?:[\s]*<option value="(?P<value>[^"]*)">
(?P<content>[^<]*)</option>)*[\s]*</select>%', 
$contents, $result, PREG_SET_ORDER);

How should I proceed?

+2  A: 

The pattern

<option value="(.*?)">(.*?)</option>

Should grab out all the data you need. Then you just need to iterate over the matches to build the array you desire.

David Caunt
There are several select tags, but only one that have the option-tags I need.
Daniel W
+3  A: 

You should better use a real parser:

$doc = new DOMDocument();
$doc->loadHTML($str);

$xpath = new DOMXPath($doc);
$result = array('value'=>array(), 'content'=>array());
foreach ($xpath->query('//body/select[count(@*)=0]/option') as $node) {
    $result['value'][] = $node->getAttribute('value');
    $result['content'][] = $node->textContent;
}
var_dump($result);
Gumbo
+1. Regex cannot parse HTML. Do not use regex to parse HTML. Do not pass Go. Do not collect £200.
bobince
A better solution for sure. I had (mistakenly) assumed he just had one select element to parse.
David Caunt