2017-04-05 28 views
0

Ich habe eine URL innerhalb eines Daten-Attribut und ich brauche die erste zu erhalten:Wie erhält man Datenattributwert?

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

Ich habe viele Antworten wie this one und this one gelesen, aber ich bin kein PHP-Typ.

Ich war mit dieser die erste img zu bekommen, aber jetzt brauche ich die eigentlichen Daten Attributwert anstelle

<?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(); ?>"> 

Antwort

3

Verwenden DOMDocument die HTML zu analysieren, erhalten die Elemente img-Tags entsprechen, und erhalten das data-flickity-lazyload Attribut die erste img tag:

... 
$DOM = new DOMDocument; 
$DOM->loadHTML($custom_image); 
$items = $DOM->getElementsByTagName('img'); 

$mySrc = $items->item(0)->getAttribute('data-flickity-lazyload'); 
+0

hey vielen dank, soll ich das dann tun? $ mySrc = $ items [0] -> getAttribute ('daten-flickity-lazyload');

+0

Geben Sie weitere Informationen zu Ihrem Fehler an: Wird eine Warnmeldung angezeigt? Was ist der Inhalt von $ mySrc? Was ist der Inhalt von $ custom_image, wenn Sie es in das DOMDocument laden? – gmc

+0

Wenn ich echo $ custom_image gebe, bekomme ich '

' –

Verwandte Themen