我希望从整个html加载中提取值(我只是简化为相关数据),有多个'select'元素,并且只想提取那些'name'元素匹配的元素名字'aMembers'。因此,我想要检索的结果值是5,10,25和30(见下文),如何使用preg_match实现此目的?
<DIV id="searchM" class="search"><select name="aMembers" id="aMembers" tabIndex="2">
<option selected="selected" value="">Data 3</option>
<option value="5">A name</option>
<option value="10">Another name</option>
</select>
</DIV>
<DIV id="searchM" class="search"><select name="bMembers" id="bMembers" tabIndex="2">
<option selected="selected" value="">Data 2</option>
<option value="15">A name</option>
<option value="20">Another name</option>
</select>
</DIV>
<DIV id="searchM" class="search"><select name="aMembers" id="Members" tabIndex="2">
<option selected="selected" value="">Data 1</option>
<option value="25">A name</option>
<option value="30">Another name</option>
</select>
</DIV>
答案 0 :(得分:-1)
我会尝试将此任务分为两个步骤:
这种方式会更容易:
$str = 'your HTML code here...';
preg_match_all('|<select name="aMembers".*?</select>|ms', $str, $matches);
foreach ($matches[0] as $select) {
preg_match_all('|value="(.+?)"|', $select, $matches2);
var_dump($matches2[1]);
}