1

I am trying to parse HTML headers for my pages and add an ID to match the content. I've managed to do it with find and replace but it fails when any additional HTML attributes are present...

Heres whats in the array of headers to change

[
    'find' => sprintf('<h%u>%s</h%u>', $level, $title, $level),
    'replace' => sprintf('<h%u id="%s">%s</h%u>', $level, slugify($title), $title, $level),
    'slug' => slugify($title)
]

Later on the replace is then done...

foreach ($parsed_content as $fix) {
    $content = str_replace($fix['find'], $fix['replace'], $content);
}

This isn't the best way to do it I know but it was just a test, now to make it work properly I think I just need to use a regular expression instead of a standard str_replace call.

I am using DOMDocument, would there be an alternative way to do it in there maybe?

Edit: I'm trying to use xpath to mainipulate the html. I've got a simple loop to get the data but i'm not sure how to apply the code to the actual docoument. Any ideas? Heres what I have atm

$dom = new DOMDocument;
$dom->loadHTML($the_dom);
$xpath = new DOMXPath($dom);

$elements = $xpath->query('(//h1|//h2|//h3|//h4|//h5)');

foreach ($elements as $index => $element) {

    $element->setAttribute('id', sanitize_title($element->textContent));
    pr($element);

}
Callum
  • 1,136
  • 3
  • 17
  • 43
  • 3
    This legendary post will live, once again! http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Frederik.L Nov 05 '15 at 04:05

1 Answers1

-1

My suggestion would be to use JS to accomplish this.

First, set JS variables with the PHP variables' values:

var level = <?= $level ?>; // inject PHP into JS
var title = <?= $title ?>;

Then, use JS (or jQuery) to replace the content:

$("h" + level).text(title);

I think this method of manipulating the DOM from the client-side is easier than using regex from a server-side language.

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94