转换正则表达式以在php preg_replace中使用

时间:2014-06-11 10:44:06

标签: php regex

我试图在PHP中使用以下正则表达式时碰到了一堵砖墙。

<li.*?class="(.*?gchoice.*?)">((.|\n)*?)<\/li>

这在以下测试中正常工作,当我在基于jquery的测试器中尝试时,仅选择嵌套列表项

 <li class="gfield gfield_contains_required" id="field_1_17">
<label class="gfield_label">Categories<span class="gfield_required"> (Required) </span>        </label>
<div class="ginput_container">
<ul id="input_1_17" class="gfield_checkbox">
<li class="gchoice_17_1">
    <input type="checkbox" id="choice_17_1" value="First Choice" name="input_17.1">
    <label id="label_17_1" for="choice_17_1">First Choice</label>
</li>
</ul>
</div>
</li>

然而在PHP中,无论我如何尝试逃避角色,无论我尝试使用哪个分隔符,它都不想工作。据我所知,我只需要在展台和展台上使用一个/或#,但是当它不起作用时我也试图转移括号,但它仍然无法正常工作。

1 个答案:

答案 0 :(得分:6)

不要使用正则表达式。

$dom = new DOMDocument();
$dom->loadHTML($your_html_source);
$xpath = new DOMXPath($dom);
$target = $xpath->query("//li[contains(@class,'gchoice')]");
foreach($target as $node) {
    var_dump($node); // do something
}
相关问题