PHP Classes

File: example/example_extract_data_attribute.php

Recommend this page to a friend!
  Classes of Lars Moelleken   Simple HTML DOM   example/example_extract_data_attribute.php   Download  
File: example/example_extract_data_attribute.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Simple HTML DOM
Manipulate HTML elements using DOMDocument
Author: By
Last change: Update of example/example_extract_data_attribute.php
Date: 1 year ago
Size: 750 bytes
 

Contents

Class file image Download
<?php

use voku\helper\HtmlDomParser;

require_once
'../vendor/autoload.php';

$templateHtml = '
<div>
    <div class="inner">
        <ul>
          <li><img alt="" src="none.jpg" data-lazyload="/pc/aaa.jpg"></a></li>
          <li><img alt="" src="none.jpg" data-lazyload="/pc/bbb.jpg"></a></li>
        </ul>
    </div>
</div>
'
;

$htmlTmp = HtmlDomParser::str_get_html($templateHtml);
$data_attribute = [];

foreach (
$htmlTmp->find('.inner img') as $meta) {
    if (
$meta->hasAttribute('data-lazyload')) {
       
$data_attribute[] = $meta->getAttribute('data-lazyload');
    }
}

// dump contents
/** @noinspection ForgottenDebugOutputInspection */
var_export($data_attribute, false);

/*
[
    0 => '/pc/aaa.jpg',
    1 => '/pc/bbb.jpg',
]
 */