php中的preg_match_all()不会显示所有结果
我正在尝试匹配每个开始和结束自定义标记php中的preg_match_all()不会显示所有结果,php,regex,pcre,Php,Regex,Pcre,我正在尝试匹配每个开始和结束自定义标记,以及它们在普通html标记中嵌入的位置,如以下示例中所示: $str =<<<'EOS' <xyz id="x464CaqYxUMjG7RJk4yXa8qY" data-arg="x=ktvBDojzvthKO9OOBzQLt6pi"> <xyz id="x" data-html> <h2>Security, Comfort, & C
,以及它们在普通html标记中嵌入的位置,如以下示例中所示:
$str =<<<'EOS'
<xyz id="x464CaqYxUMjG7RJk4yXa8qY" data-arg="x=ktvBDojzvthKO9OOBzQLt6pi">
<xyz id="x" data-html>
<h2>Security, Comfort, & Convenience</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque dicta magni amet atque doloremque velit unde adipisci omnis hic quaerat.</p>
<p><xyz id="z9Sjvxxop9BiQKc9HMzuk9Z8"></xyz></p>
</xyz>
</xyz>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
<xyz id="ZQpXDHuJHILTVjlRpodO9WrT" data-arg="x=ktvBDojzvthKO9OOBzQLt6pi,y=IyL8raQqbQQM65w7bPWJLRSJ">
<xyz id="x" data-html>
<h2>Security, Comfort, & Convenience</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque dicta magni amet atque doloremque velit unde adipisci omnis hic quaerat.</p>
<p><xyz id="z9Sjvxxop9BiQKc9HMzuk9Z8"></xyz></p>
</xyz>
<xyz id="IyL8raQqbQQM65w7bPWJLRSJ" data-html>
<div class="text-center IyL8raQqbQQM65w7bPWJLRSJ">
<h2>Happy Clients</h2>
<p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. </p>
</div>
</xyz>
</xyz>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
EOS;
但是不是通常应包含所有表达式的追赶的0-索引:
<?php
# your string over here
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($your_string_here, LIBXML_HTML_NOIMPLIED);
$xpath = new DOMXPath($dom);
libxml_clear_errors();
# adjust this xpath as needed
$headers = $xpath->query("//xyz/xyz/h2");
foreach ($headers as $header) {
echo $header->textContent . "<br>";
}
?>
知道我做错了什么吗
注:我的正则表达式通过在线工具传递,比如或使用单个反斜杠(
]+)>|
使用更好的方法,而不是使用解析器()。例如,以下代码段打印出
/xyz/xyz
下的每个标题(h2
):
foreach ($out[0] as $_out) {
echo "<textarea>" . htmlspecialchars($_out[0]) . "</textarea><hr />";
}
loadHTML($your\u string\u here,LIBXML\u HTML\u noimpled);
$xpath=newdomxpath($dom);
libxml_clear_errors();
#根据需要调整此xpath
$headers=$xpath->query(//xyz/xyz/h2”);
foreach($headers作为$header){
echo$header->textContent.“
”;
}
?>
在这里,唯一需要调整的是xpath查询。问题是您正在“打印”HTML结果,并且浏览器会自动解析它
例如,在firefox中点击CTRL+U
,将显示页面源代码
是这样的:
如你所见,你的数据就在那里
例如,尝试在textarea中打印结果,如下所示:
foreach($out[0]作为$\u out){
echo“.htmlspecialchars($_out[0])。”
;
}
你会得到正常的结果
下面是另一个示例:问题在于浏览器正在解析HTML输出,而不是采用it-self.interest方法。我会再仔细看看的。哎呀!布莱恩!按照哈桑的回答:echo\nl2br(\htmlspecialchars(\print\r($out,true),entnoquotes,ENCODING));
[1] => Array
(
[0] => Array
(
[0] => id="x464CaqYxUMjG7RJk4yXa8qY" data-arg="x=ktvBDojzvthKO9OOBzQLt6pi"
[1] => 10
)
...
Array
(
[0] => Array
(
[0] => Array
(
[0] => **where is the result???**
[1] => 6
)
...
<?php
# your string over here
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($your_string_here, LIBXML_HTML_NOIMPLIED);
$xpath = new DOMXPath($dom);
libxml_clear_errors();
# adjust this xpath as needed
$headers = $xpath->query("//xyz/xyz/h2");
foreach ($headers as $header) {
echo $header->textContent . "<br>";
}
?>
foreach ($out[0] as $_out) {
echo "<textarea>" . htmlspecialchars($_out[0]) . "</textarea><hr />";
}