Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Simple HTML DOM Parser - get element's hyphenated attribute value [duplicate]

Tags:

php

parsing

I'm using PHP Simple HTML DOM Parser. In the page which I want to parse the images 'src' attribute is replaced with 'data-src'. So, if I try to get the image path using the following code, it will return nothing:

$elimage = $offer->find('div.photo', 0);
$im = $elimage->last_child('a');
$img = $im->last_child('img');
$item['image'] = $img->src;

I've tried to do it like this, but it does not work either:

$elimage = $offer->find('div.photo', 0);
$im = $elimage->last_child('a');
$img = $im->last_child('img');
$item['image'] = $img->data-src;

Does anybody know if that's possible to get the value of a custom attribute and, if yes, how can that be achieved?

Thanks for helping!

like image 608
cycero Avatar asked Sep 23 '26 07:09

cycero


2 Answers

instead of

 $img->data-src;

use

$img->{'data-src'};

taken from here

Anyway, I was about to find a solution to this kind of problem and stumble here then suddenly got an idea.

Just put it in variable first:

$dataSrc='data-src';
$item['image'] = $img->$dataSrc;
like image 35
Ardeus Avatar answered Sep 26 '26 01:09

Ardeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!