Converting a URL to a clickable hyperlink

October 2016

With regular expressions, it is possible to transform any hyperlink URL of the form
http://URL (or ftp://URL) or beginning with www. :

$message = eregi_replace("((([ftp://])|(http(s?)://))((:alnum:|[-\%\.\?\=\#\_\:\&\/\~\+\@\,\;])*))","<a href = '\\0' target='_blank'>\\0</a>",$message);

$message = eregi_replace("(([^/])www\.|(^www\.))((:alnum:|[-\%\.\?\=\#\_\:\&\/\~\+\@\,\;])*)","\\2<a href = 'http://www.\\4' target='_blank'>www.\\4</a>",$message);

Related :

This document entitled « Converting a URL to a clickable hyperlink » from CCM ( is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.