php simple html dom parse img html5 attributes?
Asked Answered
N

4

13

How to use simple html dom parse img html5 attribute: data-original

$htmls = '<img class="lazy" alt="Nubifragio a Verbania , ferite 2 turiste  Gravi danni, chiesto stato di calamità    foto" title="Nubifragio a Verbania , ferite 2 turiste  Gravi danni, chiesto stato di calamità    foto" data-original="http://www.repubblica.it/images/2012/08/26/130634575-506cc9ae-11b8-4a53-920c-539a3811e46b.jpg" src="http://www.repubblica.it/static/images/homepage/2012/lazy.png" width="130" height="98" style="display: inline; ">';
$html = str_get_html($htmls);
$fata = $html->find('img'); 
foreach($fata as $newimage){
    echo $newimage->data-original; //0
    echo $newimage->src; //http://www.repubblica.it/static/images/homepage/2012/lazy.png
}

I could get the attribute src, but data-original return 0

Nucleoprotein answered 26/8, 2012 at 15:7 Comment(3)
You're saying $newimage->data - original;Inaccurate
@Stephen Sarcsam Kamenar, yes, how to get this value use simple html dom?Nucleoprotein
Try $newimage['data-original'];Inaccurate
M
27
$newimage->data-original;

means

$newimage->data - original;

A way round this is to try:

$property = 'data-original';
$newimage->$property;

or, to use the alternative syntax:

$newimage['data-original'];
Multicellular answered 26/8, 2012 at 15:13 Comment(3)
right, in php it should be $newimage->{'data-original'};, thanks.Nucleoprotein
I realize this is old but I came across this, submitted an edit for this to use the proper syntax on the final line.Freezer
perfect solution, really helped me.Doha
T
7

Just an FYI:

I tried:

$newimage['data-original'];

but this is what worked for me:

$property = 'data-original';
$newimage->$property;
Thromboplastic answered 25/10, 2014 at 4:59 Comment(2)
The alternative syntax didn't work for me neither, but the other one did the trick.Lucent
Same, got an error saying I couldn't use a Simple HTML DOM element as an array. Ended up using $newImage->{'data-original'} syntax which worked OK.Sperling
G
0

Hope this will work

$thumbs= $video->find('img[class=image]', 0)->{data-original};

Graben answered 24/5, 2018 at 4:16 Comment(0)
K
0

Using braces would be more efficient:

$newimage->{'data-original'};

Another Example:

<?php
   class foo {
     var $bar = 'I am bar.';
     var $arr = array('I am A.', 'I am B.', 'I am C.');
     var $r   = 'I am r.';
   }

   $foo = new foo();
   $bar = 'bar';
   $baz = array('foo', 'bar', 'baz', 'quux');
   echo $foo->$bar . "\n";
   echo $foo->{$baz[1]} . "\n";

   $start = 'b';
   $end   = 'ar';
   echo $foo->{$start . $end} . "\n";

   $arr = 'arr';
   echo $foo->{$arr[1]} . "\n";

?>

Read more about variables wraped in braces: PHP documentation: Variables

Knotted answered 20/1, 2022 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.