[thelist] [php] preg_replace text links?

Kae Verens kverens at contactjuggling.org
Mon Nov 3 07:58:43 CST 2003


kris burford wrote:

>>
>> $a=preg_replace("/(http:\/\/|www|[a-zA-Z0-9-]+\.|[a-zA-Z0-9\.-]+@)(([a-zA-Z 
>> 0-9-][a-zA-Z0-9-]+\.)+[a-zA-Z0-9-\.\/\_\?\%\#\&\=\;\~\!\(\)]+)/","<a 
>> href=\"http://\\1\\2\">\\1\\2</a>",$a);
>>
>> that extracts most links and creates HTML links.
>
>
> this is great, unless they include "http://" in their text (which then 
> results in: <a href="http://http:// ...)

true. I had a few post-filters that further refined it - for emails, and 
such, for example.

there are a load of regexps available for specific purposes (http:// 
urls, etc). It is difficult to write a single regexp that handles all 
URI formats, though, so I opt to use a general filter followed by some 
post-filters. An example is to replace http:\/\/http:\/\/ with just 
http:\/\/, to replace http:\/\/(.*@[^\/]*)$ with mailto:\1, etc

Kae



More information about the thelist mailing list