0

This code uses http://querypath.org and http://php.net/manual/en/class.domelement.php

What I would like to do is get all tags in $types and go through $html and save the output to an array in order of that tags appearance in html e.g

.imageBlock,h1,h2,p,h5,h1,h2,p,.imageblock,h4 

this is the order the final array must have how would I do this?

$html = <<<html
<section>
    <div class=" imageBlock" style="background: url('1.jpg');"></div>   
<div>
    <h1>H1 .1</h1>
    <h2>H2. 1</h2>
    <p>p .1</p>
</div>      
 <h5>Here is an H5</h5>
<div>
    <h1>H1 .2</h1>
    <h2>H2. 2</h2>
    <p>p .2</p>
</div>
<div class="imageBlock" style="background: url('2.jpg');"></div>    

<h4>Test</h4>
<section>
html;

$types = ['h1','h2','h3','h4','h5','h6','p', '.imageBlock'];

foreach($types as $type) {
    //Create a varible for each tag
    ${"$type"} = htmlqp($html, $type);
}

foreach($types as $type) {
    $types = (${"$type"}); //Easier to use varible
    $nodes = []; //Array to hold elements in order of appperance not sure if in correct place?
    foreach($types as $tag) {
        if($types->length !== 0) {
            //Show all elements
            for($i = 0 ; $i< $types->length; $i++) {
                echo $type;
                var_dump($types->get()[$i]);
            }
        }
    }
}


?>

0 Answers0