2

i have this html from stagram :

<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">

i need find class photoeach and extract id 352034826703915686_176859145

i did with regex but no luck , so im trying do it with domdocument

i followed step from Getting DOM elements by classname

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

but i cant firgure it out how i can extract ID

Community
  • 1
  • 1
madman
  • 319
  • 2
  • 6
  • 19

1 Answers1

4

As Dave already mentioned you are not really that far off:

$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>';
}

Demo: http://codepad.viper-7.com/xEdYLr

Note that I have changed the contains selector of the class to only match exact matches, otherwise the photoeachinner would also be matched. If this is not what you want you can easily revert that change.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262