我试图通过将regexpreg_match_all与class标记匹配来获得特定的HTML代码部分,但它返回空数组。
这是我想从完整的html中获得的html部分
<div class="details">
<div class="title">
<a href="citation.cfm?id=2892225&CFID=598850954&CFTOKEN=15595705"
target="_self">Restrictification of function arguments</a>
</div>
</div>
我在哪里使用regex
preg_match_all('~<div class=''details''>'s*(<div.*?</div>'s*)?(.*?)</div>~is', $html, $matches );
注意:$html变量包含我要搜索的整个html。
谢谢。
您在正则表达式中查找的是单引号,而不是$html
中的双引号。
您的正则表达式应该如下所示:
'~<div class="details">'s*(<div.*?</div>'s*)?(.*?)</div>~is'
或更好:
'~<div class=[''"]details[''"]>'s*(<div.*?</div>'s*)?(.*?)</div>~is'
最好使用DOM
方法!
<?php
$html = '<div class="details">
<div class="title">
<a href="citation.cfm?id=2892225&CFID=598850954&CFTOKEN=15595705"
target="_self">Restrictification of function arguments</a>
</div>
</div>';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$divs = $xpath->query('//div[@class="title"]');
print_r($divs);
?>