1

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.

zt1983811
  • 1,011
  • 3
  • 14
  • 34
Jyoti Sharma
  • 994
  • 5
  • 12

1 Answers1

0

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.

Jyoti Sharma
  • 994
  • 5
  • 12