php DOMDocument查找类提取id值

时间:2012-12-23 12:11:40

标签: php class element extract domdocument

我有来自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

1 个答案:

答案 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也将匹配。如果这不是您想要的,您可以轻松恢复该更改。