A regex to ensure all links have a target = "_blank"
I have a textarea that uses CKEditor to generate HTML. I want all links the user enters to have target="_blank"
. I thought I needed to do two regex validation: replace either target="..."
with target="_blank"
, and the other just insert the target attribute where the target attribute doesn't exist. I haven't made much progress:
// where target attribute doesn't exist, add it
preg_replace("/<a(\s*(?!target)([\w\-])+=([\\"\'])[^\\"\']+\3)*\s*\/?>/", "<a target="_blank"$1>", $input_lines);
This works in this simple case:
<a href="#">one</a> ---> <a target="_blank" href="#">one</a>
It doesn't work for <a href="#" alt="hello">one</a>
, I'm not sure why, but then I usually don't do this complicated with regexes.
Also, how would you replace the existing one target="..."
(for example, target="_parent
") with strictly target="_blank
"?
source to share
You can safely use PHP DOM with XPATH to set attributes or change existing ones in all tags <a>
like this:
$html = <<<DATA
<a href="somelink.html" target="_blank"><img src="myimage.jpg" alt="alt" title="sometitle" /></a>
<a href="somelink1.php" target="_parent">link_no1</a>
<a href="somelink2.php">link_no2</a>
<a href="someimage.jpg"><img src="image2.png"></a>
DATA;
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
$links = $xpath->query('//a');
foreach($links as $link) {
$link->setAttribute('target', '_blank');
}
echo $dom->saveHTML();
See IDEONE demo
Output:
<a href="somelink.html" target="_blank"><img src="myimage.jpg" alt="alt" title="sometitle"><a href="somelink1.php" target="_blank">link_no1</a><a href="somelink2.php" target="_blank">link_no2</a><a href="someimage.jpg" target="_blank"><img src="image2.png"></a></a>
source to share
A slightly different approach.
Remove all items first target="..."
. Perhaps replace with \btarget="[^"]*"
nothing or just one space.
Then add the items you want target="_blank"
. Perhaps replace <a
with <a target="_blank"
.
But be careful with these text replacements in unexpected places in the file. As noted in the comment on the question, it is almost always best to use a proper HTML / XML parser.
source to share