Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract Nested Tag Using PHP

Tag hierarchy in a webpage :

<body>
   <div id='header'>
      <h2>.....</h2>
   </div>
   <div id='main'>
      <h2>...</h2>
      //Some other content
      <h2>...</h2>
   </div>
   <div id='footer'>
      <h2>.....</h2>
   </div>
</body>

[PROBLEM : ] From the above hierarchy structure of a webpaege, I want to extract only the <h2> tags which are inside the <div id=’main’>. Can someone please please help me out ?

What I have tried is…. using HTML DOM of php $h2Tags = $htmlDom->getElementsByTagName('h2');, but this gives me all the <h2> tag which are outside of main div as well. Please guide me to a solution.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

h2_tag below will get list of h2s in main div:

var div_m = document.getElementById("main");
var h2_tag = div_m.getElementsByTagName('h2');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading