I'm adding HTML to a database table and loading it dynamically and displaying it on my page. This part works.
Now I've changed the form where I insert the HTML. I want to scan through to HTML content that's being added and automatically add target="_blank" to the <a> tags if they aren't there already, some HTML may have multiple <A> tags some may have none. Here's what I got:
Code: Select all
$matches;
$skipper = "";
if (preg_match_all("/<a\s*[^>]*>/si",$html,&$matches)) { //Does the HTML contain a link..
for($i=0;$i<count($matches[0]);$i++) { //for each link
if(!(preg_match("/<a[^>]*\starget=[^>]*>/",$matches[0][$i]))) { //Link doesn't load in an empty page..fix it.
$html = preg_replace("/(".$skipper.".*?<a\s)/sim","'$1 target=\"_blank\" '",$html);
}
$skipper .= ".*?<a[^>]*>";
}
}
Thanks for any advice or help!
Jason.