UrlLinker is a PHP module for converting plaintext to HTML, and any URLs in the text into HTML links.

A Stackoverflow.com question prompted me to consider the difficulty of such a task. Initially, it seemed easy, but like an itch you just have to scratch, I kept coming back to it, to fix just one more little thing. I'm pretty happy about the final result.

Source: UrlLinker.php (download)

Example of use:

print(htmlEscapeAndLinkUrls($text));

Longer example

Source: UrlLinker-example.php (Download)

<!doctype html>
<html>
<head>
<title>UrlLinker Example</title>
</head>
<body>
<p>
<?php

require("UrlLinker.php");

$text = <<<EOD
Here are some URLs:
stackoverflow.com/questions/1188129/pregreplace-to-detect-html-php
Here's the answer: http://www.google.com/search?rls=en&q=42&ie=utf-8&oe=utf-8&hl=en. What was the question?
A quick look at http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax is helpful.
There is no place like 127.0.0.1! Except maybe http://news.bbc.co.uk/1/hi/england/surrey/8168892.stm?
Ports: 192.168.0.1:8080, https://example.net:1234/.
Beware of Greeks bringing internationalized top-level domains: xn--hxajbheg2az3al.xn--jxalpdlp.
And remember.Nobody is perfect.

<script>alert('Remember kids: Say no to XSS-attacks! Always HTML escape untrusted input!');</script>
EOD;

print(
nl2br(htmlEscapeAndLinkUrls($text)));

?>
</p>
</body>
</html>

Output:

Here are some URLs:
stackoverflow.com/questions/1188129/pregreplace-to-detect-html-php
Here's the answer: www.google.com/search. What was the question?
A quick look at en.wikipedia.org/wiki/URI_scheme is helpful.
There is no place like 127.0.0.1! Except maybe news.bbc.co.uk/1/hi/england/surrey/8168892.stm?
Ports: 192.168.0.1:8080, example.net:1234/.
Beware of Greeks bringing internationalized top-level domains: xn--hxajbheg2az3al.xn--jxalpdlp.
And remember.Nobody is perfect.

<script>alert('Remember kids: Say no to XSS-attacks! Always HTML escape untrusted input!');</script>

License

CC0 To the extent possible under law, Søren Løvborg has waived all copyright and related or neighboring rights to UrlLinker.