note 34932 rejected from function.preg-replace by pollita

From: Date: Tue, 12 Aug 2003 15:09:23 +0000
Subject: note 34932 rejected from function.preg-replace by pollita
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-54016@lists.php.net to get a copy of this message
Note Submitter: adamh@densi.com ---- The ultimate URL regex: I was googling for one that does everything I want, and was looking through mailing list discussions, when I noticed that all the urls in the mailing list text-turned-HTML were matched perfectly. The mails were parsed by MHonArc, http://www.oac.uci.edu/indiv/ehood/mhonarc.html, by Eric D. Friedman. I immediately looked up the source code. I also added a basic "www.x" match. The following code is GPL // Urls copied from mhtxtplain.pl in MHonArc // http://www.oac.uci.edu/indiv/ehood/mhonarc.html $Url = '(http://|https://|ftp://|afs://|wais://|telnet://|ldap://'. '|gopher://|news:|nntp:|mid:|cid:|mailto:|prospero:)'; // Escaping characters is hell. $HUrlExp = <<<EOT $Url(?:&(?![gl]t;)|[^\s\(\)\|<>"'\&\\0-\\037])+ EOT; $HUrlExp .= <<<EOT [^\.?!;,"'\|\[\]\(\)\s<>\&\\0-\\037] EOT; $repls["@($HUrlExp)@i"] = '<a href="$1">$1</a>'; unset($Url, $HUrlExp); // (?<!\\/) is a negative look-behind assertion, only for fixed-width // Unfortunately this matches "www.bleh" in the middle of words. Too bad. $repls['/(?<!\\/)www\.(%[\dA-Fa-f]{2}|'. '[-.!~*\';\\/?:@&=+$,\w])+/'] = '<a href="http://$0">$0</a>'; $text = preg_replace(array_keys($repls), array_values($repls), $text);

« previous php.notes (#54016) next »