此代码使用http://querypath.org和http://php.net/manual/en/class.domelement.php
我想要做的是获取$ types中的所有标签并通过$ html并将输出保存到数组中,以便在html中显示该标记的顺序 例如
.imageBlock,h1,h2,p,h5,h1,h2,p,.imageblock,h4
这是最终数组必须具有的顺序我将如何做到这一点?
$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]);
}
}
}
}
?>