我有来自stagram的这个html:
<div id="photo351321902758808423_176859145" class="photoeach">
<div class="photoeachinner">
<div class="left">
<div class="photowrapper">
<div class="infomation_box clearfix">
<div class="profimage_small">
<div id="photo351295515670923844_176859145" class="photoeach">
<div class="photoeachinner">
<div class="left">
<div class="photowrapper">
<div class="infomation_box clearfix">
我需要找到类photoeach并提取id 352034826703915686_176859145
我使用正则表达式,但没有运气,所以我尝试使用domdocument
我跟着一步 Getting DOM elements by classname
$dom = new DomDocument();
$dom->load($filePath);
$finder = new DomXPath($dom);
$classname="photoeach";
$nodes = $finder->query("//*[contains(@class, '$classname')]");
但我无法确定如何提取ID
答案 0 :(得分:3)
正如戴夫已经提到的那样你并不是那么遥远:
$dom = new DomDocument();
$dom->load($filePath);
$finder = new DomXPath($dom);
$classname="photoeach";
$nodes = $finder->query("//*[@class = '$classname']");
foreach ($nodes as $node) {
echo 'Id: ' , substr($node->getAttribute('id'), 5) , '<br>';
}
演示:http://codepad.viper-7.com/xEdYLr
请注意,我已将类的contains
选择器更改为仅匹配完全匹配,否则photoeachinner
也将匹配。如果这不是您想要的,您可以轻松恢复该更改。