PHP Classes

File: example/example_modify_attribute.php

Recommend this page to a friend!
  Classes of Lars Moelleken   Simple HTML DOM   example/example_modify_attribute.php   Download  
File: example/example_modify_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: [+]: add one more example
Date: 1 year ago
Size: 641 bytes
 

Contents

Class file image Download
<?php

use voku\helper\HtmlDomParser;

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

$html = '<html><div id="p1" class="post">foo</div><div class="post" id="p2">bar</div></html>';

$document = new HtmlDomParser($html);

foreach (
$document->find('div') as $e) {
   
$attrs = array();
    foreach (
$e->getAllAttributes() as $attrKey => $attrValue) {
       
$attrs[$attrKey] = $attrValue;
       
$e->$attrKey = null;
    }

   
ksort($attrs);

    foreach (
$attrs as $attrKey => $attrValue) {
       
$e->$attrKey = $attrValue;
    }
}

echo
$document->html(); // <html><div class="post" id="p1">foo</div><div class="post" id="p2">bar</div></html>