Adding a target to link tags, as long as the href attribute doesn't contain a certain word
I created this function:
<?php
function target_links( $html )
{
$pattern = "/<(a)([^>]+)>/i";
$replacement = "<\\1 target=\"_blank\"\\2>";
$new_str = preg_replace($pattern,$replacement,str_replace('target="_blank"','',$html));
return $开发者_开发知识库new_str;
}
?>
The goal is to add a target="_blank" to all the link tags.
Now my problem is that I need to skip all link tags where the href
attribute contains a specific word, but I can't seem to find the proper combination. Can you guys help me?
I'm not to sure about the "not failing because of broken HTML", but if you can get DomDocument to accept your html, try something like:
<?php
$dom = new DOMDocument();
$dom->loadHtml('<html>
<a href="...protected...">some link</a>
<a href="...change me...">some link</a>
</html>');
$xpath = new DOMXpath($dom);
foreach ($xpath->query('//a[not(contains(@href, "protected"))]') as $node) {
$node->setAttribute('target', '_blank');
}
header('Content-Type: text/html; charset="UTF-8"');
echo $dom->saveHtml();
A regex solution can look like this:
<(a)(?!.*?href="[^"]*SPECIFICWORD)([^>]+)>
A negative lookahead (?!.*?href="[^"]*SPECIFICWORD)
is used to check if the "SPECIFICWORD" is within the href attribute, if yes the regex does not match.
See here online on Regexr
精彩评论