tags:

views:

111

answers:

1

How would a regex look that makes text that starts with either http or www clickable?

My current bbcode:

function bbcode($text) {
$text = htmlspecialchars($text);
$text = nl2br($text);

$find = array(
              "'\[b\](.*?)\[/b\]'is",
              "'\[i\](.*?)\[/i\]'i",
              "'\[url\](.*?)\[/url\]'i"
              );

$replace = array(
              "\\1",
              "\\1",
              "\\1"
                );

$text = preg_replace($find, $replace, $text);

return $text;
}

As you can see I use [url]link[/url] for links ATM.

Thanks in advance.

P.S. the html in replace array wont show...

+3  A: 

Here's a nice simple way:

Find: (http://[^ ]+)
Replace: <a href="\\1">\\1</a>

Find: (www\.[a-zA-Z0-9\-]\.[^ ]+)
Replace: <a href="\\1">\\1</a>
yjerem