このコードでは、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]);
}
}
}
}
?>