Wednesday, November 11, 2009

Rewrite URL to HTML with PHP

When presenting an article or other information it's convenient for readers to be able to click on links you are referring to. With regular expressions the composers are able to write clickable links without know any HTML.

In the example below we are getting the plain text in the variable $str.

Step one, which protocols are allowed? By adding more protocols to the array you're accepting them to be rewritten.
// Protocols
$protocols = array("http://", "https://", "ftp://");
// Imploding the array to fit the regex
$protocols = str_replace("/", "\/", implode("|", $protocols));

Step two, adding http:// to URLs without a specified protocol, like
$str = preg_replace("/(?<=^|\(|\s)(www\.[A-Za-z0-9-_]+\.[A-Za-z\.]{2,6}(?:[\/\?].*)?)(?=[\.,!\?]*(?:\s|\(|\)|$))/U", "http://$1", $str);
Step three, replace all accepted URLs with HTML

$str = preg_replace("/(?<=^|\(|\s)({$protocols})((?:[A-Za-z0-9-_]+\.)?[A-Za-z0-9_-]+\.[A-Za-z\.]{2,6}(?:[\/\?].*)?)(?=[\.,!\?]*(?:\s|\(|\)|$))/U", "<a href=\"$1$2\">$2</a>", $str);
This code will work with the following ways to write URLs:

(example:,! - The dot won't be a part of the link - The dot won't be a part of the link - The last dot won't be a part of the link

But not with:

No comments:

Post a Comment