3

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
Kara
  • 6,115
  • 16
  • 50
  • 57
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
  • I don't always post that link, but when I do it's because I'm too lazy to find a more exact dupe: [Best methods to parse HTML in PHP](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php) (doesn't explain much though). While WP probably and other implementations often use a simpler approach. – mario Dec 23 '11 at 22:14

1 Answers1

3

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

craniumonempty
  • 3,525
  • 2
  • 20
  • 18
  • Thanks for the answer. Is there a way to order all of my headings (so I can make a list, as shown in my answer)? – Kevin Ji Dec 24 '11 at 18:03
  • @mc10 Ok, you have two options (but I"m unsure of one), you can use the "*" (in the getElementsByTagName function) to pull all tags and just sift out the ones you want. Use `$val->tagName` in the foreach loop to test what tags you are looking for. – craniumonempty Dec 25 '11 at 09:43
  • @mc10 oh, I didn't put it. Sorry, still have my mind on another project. The other option was to try the asterisk as a wild card, but I didn't see anything on the net about it. – craniumonempty Dec 25 '11 at 22:33