Here is the code i am using
function parseURL($text) {
$regex = "#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#iS";
preg_match_all($regex, $text, $matches);
foreach($matches[0] as $pattern){
$text = str_replace($pattern, "<a href=\"$pattern\" target=\"_blank\">$pattern</a> ", $text);
}
return $text;
}
For some reason my regex is outputting the following results: (bold = linked)
www.domain.com
http://www.domain.com
so it works fine except if it contains both http and www at which point it only links from the www part onward.
any idea why?
EDIT
For anyone reading this requiring the fix, here is the working code thanks to Wiktor Stribiżew..
function parseURL($text) {
$regex = "@\b(([\w-]+://?|www[.])[^\s()<>]+(?:\(\w+\)|([^[:punct:]\s]|/)))@i";
$subst = "<a href='$0' target='_blank'>$0</a>";
$text = preg_replace($regex, $subst, $text);
return $text;
}
preg_replacedirectly. Also,Smakes no sense here (even if you meant to uses, DOTALL). See this PHP demo.