Remy
Remy

Reputation:

Regex http/www to links

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...

Upvotes: 1

Views: 607

Answers (1)

Paige Ruten
Paige Ruten

Reputation: 176655

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>

Upvotes: 4

Related Questions