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);
}