tags:

views:

55

answers:

1

Hi all,

I'm using this code

$patterns = "!((http:/{2}[\w\.]{2,}[/\w\-\.\?\&\=\#]*)|(www\.[/\w\-\.\?\&\=\#]*)|([a-zA-Z0-9-\.]+(com|us|co.il)[^\s]*))!e";
 return preg_replace($patterns, "'<a class=\"highlight boughtAt\" href=\"\\1\" title=\"\\1\" target=\"_blank\">'.(strlen('\\1')>=$chr_limit ? substr('\\1',0,$chr_limit).'$add':'\\1').'</a>'", $url);

but my problem is that if I get the www. or just .com I get in the href= the prefix of the domain that i'm in. How can i avoid it? just get a working link?

Tx in advance, Roy

A: 

You should be able to use a lookbehind assertion - (?<=...) then always add in the http:// in your link:

$patterns = "!(((?<=http:/{2})[\w\.]{2,}[/\w\-\.\?\&\=\#]*)|(www\.[/\w\-\.\?\&\=\#]*)|([a-zA-Z0-9-\.]+(com|us|co.il)[^\s]*))!e";
return preg_replace($patterns, "'<a class=\"highlight boughtAt\" href=\"http://\\1\" title=\"\\1\" target=\"_blank\">'.(strlen('\\1')>=$chr_limit ? substr('\\1',0,$chr_limit).'$add':'\\1').'</a>'", $url);
Greg