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!

2
  • 1
    Get src by regular expressions form $img Commented Jan 13, 2012 at 11:52
  • Thanks for your help. Grabbed the source with the following regexp '$\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]$i'. They had absolute URL-s. Commented Jan 13, 2012 at 13:00

4 Answers 4

4

instead of

 $img->data-src;

use

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

taken from here

Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

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

Comments

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.