How to extract headings of a page using PHP (specifically WordPress)

Is it possible, using PHP, to extract the headings (h1, h2, etc.) from a page using PHP, and list them on the same page in an unordered list? A WordPress-specific solution is fine, but a general PHP solution is welcome as well.

EDIT: What I want is something of the form

h1
  h2
    h3
    h3
  h2
    h3
    h3

Related posts

Leave a Reply

1 comment

  1. In php, you could use xml manipulation:

    http://www.php.net/manual/en/domdocument.getelementsbytagname.php

    Haven’t tested yet, but something like this for h1:

    $dom = new DOMDocument();
    
    @$dom->loadHTML(file_get_contents('htmlfile.htm'))
    
    $h1 = $dom->getElementsByTagName('h1');
    
    foreach ( $h1 as $val ){
        echo $val->property->__toString();
    }
    

    I’m not quite sure on the functions, and I’m not going to be able to test (until probably tomorrow)… I got the tostring from http://br.php.net/manual/en/class.domelement.php#98851