note 34932 added to function.preg-replace
| From: | adamh at densi dot com | Date: | Tue, 12 Aug 2003 13:54:31 +0000 |
| Subject: | note 34932 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-54012@lists.php.net to get a copy of this message | ||
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);
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34932
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34932&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34932&report=yes