Here’s a very simple PHP function that will parse text and force fully constructed links to open in a new window. If you’re building a comment area (or something similar) for your website it’s likely that you want to retain an open page to minimise your click-through-rate, and this function will assist to serve that purpose.
If you are parsing plain text links it’s likely that you will want to use the “make your links clickable” function and adjust it accordingly (on line 15 simple add target=”_blank” into the link structure).
<?php
function newwin($text) {
$text = preg_replace('/<a (. ?)>/i', "<a $1 target='_blank' rel='external'>", $text);
return $text;
}
$content = "<a href=\"http://www.internoetics.com\">This link</a> will open in a new window";
echo newwin($content);
?>
If you liked this article, you may also like:
Recent Comments