繁体   English   中英

PHP DOMDocument查找类提取ID值

[英]php DOMDocument find class extract id value

我从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

我遵循了通过类名获取DOM元素的步骤

$dom = new DomDocument();
$dom->load($filePath);
$finder = new DomXPath($dom);
$classname="photoeach";
$nodes = $finder->query("//*[contains(@class, '$classname')]");

但我无法说明我如何提取ID

正如戴夫(Dave)所说的,您并不是那么遥远:

$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也将被匹配。 如果这不是您想要的,则可以轻松地还原该更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM