douhanzhuo6905 2017-07-24 11:26 采纳率: 100%
浏览 25
已采纳

Php Dom文件[复制]

This question already has an answer here:

I have an html content like below:

<div class="col-md-6 col-sm-6 additional-nav">
 <ul class="list-unstyled list-inline pull-right">
   <li><a href="account.html">My Account</a></li>
   <li><a href="wishlist.html">My Wishlist</a></li>
   <li><a href="checkout.html">Checkout</a></li>
   <li><a href="login.html">Log In</a></li>
   <li><a href="registration.html">Registration</a></li>
 </ul>
</div>

This HTMl content will be dynamic. Means parent element can be div , span , anchor or any other html tag.

I want to get the first element type using php DOM.

Like in the current html , parent element type is div.

How I can get it.

</div>
  • 写回答

1条回答 默认 最新

  • douchenchepan6465 2017-07-24 13:34
    关注

    I got two solutions of it.

            $block_str = '<div class="col-md-6 col-sm-6 additional-nav">
                            <ul class="list-unstyled list-inline pull-right">
                               <li><a href="account.html">My Account</a></li> 
                               <li><a href="wishlist.html">My Wishlist</a></li> 
                               <li><a href="checkout.html">Checkout</a></li> 
                               <li><a href="login.html">Log In</a></li> 
                               <li><a href="registration.html">Registration</a></li>
                           </ul>
                         </div>';
            $document = new DOMDocument;
            $document->loadHTML($block_str);
            $body_obj = $document->getElementsByTagName('body')->item(0)->childNodes;
            $first_node_name = $body_obj->item(0)->nodeName;
    

    We can get it by following document functions as well.

        $first_node_name = $document->documentElement->childNodes->item(0)->childNodes->item(0)->nodeName;
    

    So in $first_node_name, I got the first element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口