Trying to match any weird and wonderful url or hyperlink in a piece of text is fun – I found “help” all over the place, but none of them quite got it all – This was clear when using the eccentric holidays calendar for a test case in my ical upcoming events list
Eventually with the help of “The Regex Coach” (thank you very much) to test we (I’m a gemini) settled on the following code. It will find any of the following links, while excluding any text already hyper linked.
Dasher, Dancer, Prancer, Vixen, Comet, Cupid, Donner, Blixen….
The code handles urls with dashes, underscores, trailing slashes, suffixes, query strings ? and ampersands &, equal signs. It rejects urls with other funny characters.
We want regular expressions to replace EXPR in the formula below:
$text = ereg_replace( EXPR
, "<a href=\"\\0\">\\0</a>", $text);
http or https or ftp type urls:
and another for www urls (at start of line or after space)
These two lines of code deals with the following examples: