php - How to get data attribute value? -


i have url within data-attribute , need first one:

<div class="carousel-cell">     <img onerror="this.parentnode.removechild(this)"; class="carousel-cell-image" data-flickity-lazyload="http://esportareinsvizzera.com/site/wp-content/uploads/8.jpg"> </div> <div class="carousel-cell">     <img onerror="this.parentnode.removechild(this);" class="carousel-cell-image" data-flickity-lazyload="http://www.finanziamentiprestitimutui.com/wp-content/uploads/2014/09/esportazioni-finanziamento-credito.jpg"> </div> <div class="carousel-cell">     <img onerror="this.parentnode.removechild(this);"  class="carousel-cell-image" data-flickity-lazyload="http://www.infologis.biz/wp-content/uploads/2013/09/export.jpg"> </div> <div class="carousel-cell">     <img onerror="this.parentnode.removechild(this);"  class="carousel-cell-image"  data-flickity-lazyload="http://www.cigarettespedia.com/images/2/25/esportazione_horizontal_name_ks_20_s_green_italy.jpg"> </div> 

i have been reading lots of answers this one , this one not php guy.

i using first img need actual data attribute value instead

<?php       $custom_image = usp_get_meta(false, 'usp-custom-4');       $custom_image = htmlspecialchars_decode($custom_image);       $custom_image = nl2br($custom_image);       $custom_image = preg_replace('/<br \/>/iu', '', $custom_image);       preg_match('/<img.+src=[\'"](?p<src>.+?)[\'"].*>/i',$custom_image, $image);     ?>    <img src="<?php echo $image['src']; ?>" alt="<?php the_title(); ?>"> 

use domdocument parse html, elements corresponding img tags , data-flickity-lazyload attribute of first img tag:

... $dom = new domdocument; $dom->loadhtml($custom_image); $items = $dom->getelementsbytagname('img');  $mysrc = $items->item(0)->getattribute('data-flickity-lazyload'); 

Comments