PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Linkify: turning URLs into clickable links in PHP

Note: This article was originally published at Planet PHP on 27 March 2012.
Planet PHP

Turning links like www.example.com and http://twitter.com into clickable links. Sounds like an easy task, right? We'll there are a few problems that might arise, especially if the text is already html formatted.

This function first takes out all potential dangers, by extracting links and tags and replacing them with a placeholder. It than extracts all URLs and replaces them with a placeholder, storing the full html link. At the end it replaces all placeholders with the links and tags.

This is also a nice example for how to use PHP closures.