PHP:按子级内容在两个标签之间提取字符串 [英] PHP : Extracting string between two tags by childs content
本文介绍了PHP:按子级内容在两个标签之间提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下html标记:
I have this following html markup:
<ul>
<li>
<strong>Online:</strong>
2/14/2010 3:40 AM
</li>
<li>
<strong>Hearing Impaired:</strong>
No
</li>
<li>
<strong>Downloads:</strong>
3,840
</li>
</ul>
,我想从"Downloads:"
的最后一个li
中捕获3,840
.
and I want to catch 3,840
from last li
by "Downloads:"
.
您有什么建议?
我的尝试:
preg_match('/<li><strong>Downloads:<\/strong>(.*?)<\/li>/s', $s, $a);
推荐答案
我建议在这里使用HTML解析器,尤其是xpath.DOMDocument
.
I suggest use an HTML Parser here, DOMDocument
in particular with xpath.
示例:
$markup = '<ul>
<li>
<strong>Online:</strong>
2/14/2010 3:40 AM
</li>
<li>
<strong>Hearing Impaired:</strong>
No
</li>
<li>
<strong>Downloads:</strong>
3,840
</li>
</ul>';
$dom = new DOMDocument();
$dom->loadHTML($markup);
$xpath = new DOMXpath($dom);
// this just simply means get the string next on that strong tag with a text of Downloads:
$download = trim($xpath->evaluate("string(//strong[text()='Downloads:']/following-sibling::text())"));
echo $download; // 3,840
这篇关于PHP:按子级内容在两个标签之间提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文