doupi3874 2016-09-26 19:08
浏览 9

如何解析html并按照html中的apperance顺序返回数组中的元素值?

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?