What i wanted to do is to search for $BaseDomain
if it exist within the link match by regex and if true no need of opening the link in another tap. I also tried using $replacePattern1
instead of $1
and $replacePattern2
instead of $2
. The target = _blank
is never appended to the href
when the the link is not the $BaseDomain
. Please where am i wrong?
function ReplaceUrlToHtmlLink($source) {
/// my site name ///
$BaseDomain = "ivotism.com";
//URLs starting with http://, https://, or ftp://
$replacePattern1 = '/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i';
$source = preg_replace($replacePattern1, '<a href="$1" ' . (strpos('$1', $BaseDomain) !== false ?
' target="_blank"' : '') . '>$1</a>', $source);
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
$replacePattern2 = '/(^|[^\/])(www\.[\S]+(\b|$))/i';
$source = preg_replace($replacePattern2, ' <a href="http://$2" ' . (strpos('$2', $BaseDomain) !== false ?
' target="_blank"' : '') .'>$2</a>', $source);
//Change email addresses to mailto:: links.
$replacePattern3 = '/(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/i';
$source = preg_replace($replacePattern3, '<a href="mailto:$1">$1</a>', $source);
return $source;
}