Re: finding an URL in a code and creating a hypertext link with this URL
| From: | Daniel Convissor | Date: | Wed, 09 Aug 2000 17:50:04 +0000 |
| Subject: | Re: finding an URL in a code and creating a hypertext link with this URL | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-10966@lists.php.net to get a copy of this message | ||
Hi Denis:
Denis Bourdon wrote:
>
> Hello!
>
> I have a plain text which contains URLs and I'd like to display them with a
> link:
> The following string:
> http://www.php.net
>
> would be replaced with:
> <a
> href="http://www.php.net">http://www.php.net</a>
Here's the regular expression I use
$Val =
eregi_replace("(http://|https://|ftp://|gopher://|news:|mailto:)([[:alnum:]/!#$%&'()*+,.:;=?@~-]+)([[:alnum:]/!#$%&'()*+:;=?@~-])",
"<a href=\"\\1\\2\\3\">\\1\\2\\3</a>", "$Val");
URI's / URL's starting with a known transport scheme are automatically
converted to hyperlinks:
http://, https://, ftp://, gopher://, news:, mailto:
This process properly recognizes URI's which use characters authorized by RFC
2396:
A-Z, a-z, 0-9, !#$%&'()*+,-./:;=?@_~
If illegal characters are found, the hyperlink stops at the preceeding
character. Also, while "." and "," are valid characters, when they appear at
the end of a URI, we consider them punctuation rather than part of the URI.
If you have a URI ending with such, escape them using "%46" or "%44".
Enjoy,
--Dan
--
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
More than just answers. Solutions. (SM)
http://www.analysisandsolutions.com/
4015 7 Av #4, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409