Find H2 tag in a string and give it a ID
Searching stackoverflow i've found a answer for my need, but I can't figure out how to use it exactly if someone could give me a hint It would be appreciated !
Here's my need, I'm using word开发者_开发技巧press and I would to put automatic ID to <...> tags so I found "mario" who answer this:
If you have a coherent input like that, then you can use regular expressions. In this case it's both very acceptable and simple:
$html = preg_replace_callback("#<(h[1-6])>(.*?)</\\1>#", "retitle", $html); function retitle($match) { list($_unused, $h2, $title) = $match; $id = strtolower(strtr($title, " .", "--")); return "<$h2 id='$id'>$title</$h2>"; }
The id conversion needs a bit more work. And to make the regex more reliable the innter text match pattern
(.*?)
could be written as([^<>]*)
for example.H2 tag auto ID in php string
So i've tryed to apply this to my script, but that doesn't work well at all, here is my code
<?php
$html = get_the_content();
$html = preg_replace_callback("#<(h[1-6])>(.*?)</\\1>#", "retitle", $html);
function retitle($match) {
list($_unused, $h2, $title) = $match;
$id = strtolower(strtr($title, " .", "--"));
return "<$h2 id='$id'>$title</$h2>";
}
if(have_posts()) : while(have_posts()) : the_post(); //Vérifie que le contenu existe
echo $html;
endwhile;
endif;
?>
Don't use regex to solve that problem. Using domdocument:
if (empty($content)) return '';
$dom = new DomDocument();
libxml_use_internal_errors(true)
$html = '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>'.$content.'</body>
</html>';
$dom->loadHtml($html);
$hTAGs = $dom->getElementsByTagName($tag);
foreach ($hTAGs as $hTAG) {
if (!$hTAG->hasAttribute('id')) {
$title = $hTAG->nodeValue;
$id = iconv('UTF-8', 'ASCII//TRANSLIT', $title);
$id = preg_replace('/[^a-zA-Z0-9-\s]/', '', $id);
$hTAG->setAttribute('id', $id);
}
}
$content = '';
$children = $dom->getElementsByTagName('body')->item(0)->childNodes;
foreach ($children as $child) {
$content .= $dom->saveXml($child);
}
return $content;
Never, ever use RegEx for HTML, ok? Just accept this. Or read the numerous posts on here why not.
DOMDocument is ugly and evil. Use simple_html_dom instead, it's much simpler:
include 'simple_html_dom.php';
$html = str_get_html('<h2>hello</h2><h3>world</h3><h2 id='123'>how r ya</h2>');
$h2s = $html->find("h2");
foreach($h2s as $h2)
{
if(!$h2->hasAttribute("id")) $h2->id = "title";
}
echo $html->save();
精彩评论