1

I am using for parsing text simple html dom, but he cant manage this kind of accessing divs.

foreach($html->find("div") as $div)
    {
        if($div->data-zoom-image != false) 
              // some job
     }

error log:

Use of undefined constant data - assumed 'data'
Use of undefined constant zoom - assumed 'zoom'
Use of undefined constant image - assumed 'image'

it seems that elements with dash ( - ) needs to be accessed on some other way

Ivan Vulović
  • 2,147
  • 6
  • 27
  • 43

2 Answers2

11

Wrap attributes that contain dashes with {''}

foreach($html->find("div") as $div)
{
    if($div->{'data-zoom-image'} != false) 
        // some job
}

You can also use this method.

foreach($html->find("div") as $div)
{
    $property = 'data-zoom-image';
    if($div->$property != false) 
        // some job
}
slapyo
  • 2,979
  • 1
  • 15
  • 24
-1

Can use getElementByTagName. Example:

foreach($html->find("div") as $div)
{
    if($div->getElementByTagName('data-zoom-image') != false){
        // some job
    }
}
MH2K9
  • 11,951
  • 7
  • 32
  • 49