3

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!

Gordon
  • 312,688
  • 75
  • 539
  • 559
cycero
  • 4,547
  • 20
  • 53
  • 78

4 Answers4

4

instead of

 $img->data-src;

use

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

taken from here

Community
  • 1
  • 1
3

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;
Ardeus
  • 1,921
  • 3
  • 17
  • 26
1
<?php
$str= "<a data-src='http://google.com'>Hello</a>";
$var=preg_split("/data-src=\'/",$str);
//echo $var[1];
$var1=preg_split("/\'/",$var[1]);
echo $var1[0];
?>

You can use this also..

Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56
0

Thanks for the help!

I grabbed the URL from the string using the following function:

function checkURL($str) {
    $regex = '$\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]$i';

    preg_match_all($regex, $str, $result, PREG_PATTERN_ORDER);
    $A = $result[0];

    foreach($A as $a) {
        $retval .= $a; 
    }

    return $retval;
}

The URL-s I was finding were absolute and there was only one URL in the given string.

Thanks again for the hint @Rajat Singhal!

cycero
  • 4,547
  • 20
  • 53
  • 78