Helper Function : Get target of a link (PHP)

January 28th, 2014

I have found myself needing this a few times recently so it will be easiest for me to post it here for easy reference.

This function simply checks the URL to see if a “://” exists in it. If it does, it returns the _blank target. Otherwise it returns an empty string.

<?php function get_target( $link ){
	$target = "";
	if (strpos($link,'://') !== false) {
		$target = 'target="_blank"';
	return $target;
} ?>

That means you can use it like:

echo '<a href="' . $link_url . '" ' . get_target( $link_url ) . '>';
echo $link_content;
echo '</a>';

Comments are closed.

Comments have been closed for this post. If you would like to ask a question or need help, please post in the forums.