PHP make_clickable

Published 26th April 2012

So you have string that contains URL’s and you want to make them “clickable”. This is simple and it works:

function make_clickable($text) { return preg_replace('@(?<![.*">])b(?:(?:https?|ftp|file)://|[a-z].)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]@i', '[]()', $text); }

Supports normal, ftp, file and email URL’s as well as subdomains. Also it doesn’t mess with HTML a tags that already exist in the string.