note 32399 added to function.preg-replace
| From: | ed1 at anastas dot org | Date: | Mon, 26 May 2003 22:20:54 +0000 |
| Subject: | note 32399 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-49053@lists.php.net to get a copy of this message | ||
IMPROVED LINK CREATOR
I created a function which converts links also, with the help of everyone who has posted above, but
one thing seemed to be left out...
Nobody seemed to address the issue of shorthand domain names like php.net or even www.php.net so I
created a function that tries to incorporate all the comments above and allows shorthand domain
links as well.
If anyone can improve on this I would appreciate it.
NOTES:regarding shorthand domains, I only match the most common domain extension like .com or .net
(see code below for more) because I was having problems with P.O.BOX address submissions. This may
be improved on.
CODE
-----------------------------------------------------------
function html_links($text) {
$tld = "com|net|org|ws|name|info|biz|tv";
return preg_replace(
array (
// (_|^)domain.???(_|$|.$) // DOMAIN
"/(\s{1}|\A)(\w+\.)($tld)(\.)?(\s{1}|\Z)/i",
// (_|^)ftp.domain.???(_|$|.$) // SUBDOMAIN.DOMAIN
"/(\s{1}|\A)(ftp.)(\w+\.)($tld)(\.)?(\s{1}|\Z)/i",
// (_|^)???.domain.???(_|$|.$) // FTP.DOMAIN
"/(\s{1}|\A)(\w+\.)(\w+\.)($tld)(\.)?(\s{1}|\Z)/i",
// (HTTP/FTP)://DOMAIN
"/(ht|f)(tp)(s|7)?(:\/\/)?([\w+\.]+)
([\w\.\/\=\?\-\~\!\@\#\$\&]*)/i",
// EMAIL ADDRESS
"/(\s{1}|\A)((\w)+\:)?([\w\.\_\-]+)
(@)([\w\.]+)(\s{1}|\Z)/i"),
array (
// DOMAIN
" http://www.$2$3 ",
// SUBDOMAIN.DOMAIN
" ftp://$2$3$4 ",
// FTP.DOMAIN
" http://$2$3$4 ",
// (HTTP/FTP)://DOMAIN
" <A TARGET=$target HREF=\"$1$2$3$4$5$6\">$5</A> ",
// EMAIL ADDRESS
" <A HREF=\"mailto:$4$5$6\">$4$5$6</A>
"),
$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+32399
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32399&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32399&report=yes