使用类名或id使用php获取完整的'div'内容 [英] get complete 'div' content using class name or id using php

查看:112
本文介绍了使用类名或id使用php获取完整的'div'内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用php从一个文件中获得了页面源文件,其输出类似于

i got a page source from a file using php and its output is similar to

<div class="basic">

 <div class="math">

  <div class="winner">

   <div class="under">

        <div class="checker">

         <strong>check</strong>

        </div>

   </div>

  </div>

 </div>

</div>

从这个我需要只有一个特殊的'div'与整个div和内容像下面时我给出了'下'(类名)的输入。任何人建议我如何使用php

from this i need to got only a particular 'div' with whole div and contents inside like below when i give input as 'under'(class name) . anybody suggest me how to do this one using php

<div class="under">

      <div class="checker">

         <strong>check</strong>

      </div>

 </div>


推荐答案

试试这个:

Try this:

$html = <<<HTML
<div class="basic">
    <div class="math">
        <div class="winner">
            <div class="under">
                <div class="checker">
                    <strong>check</strong>
                </div>
            </div>
        </div>
    </div>
</div>;
HTML;

$dom = new DOMDocument();

$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

$div = $xpath->query('//div[@class="under"]');

$div = $div->item(0);

echo $dom->saveXML($div);

这会输出:

This will output:

<div class="under">
    <div class="checker">
        <strong>check</strong>
    </div>
</div>

这篇关于使用类名或id使用php获取完整的'div'内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆