finding an URL in a code and creating a hypertext link with this URL
| From: | Denis Bourdon | Date: | Thu, 10 Aug 2000 14:36:33 +0000 |
| Subject: | finding an URL in a code and creating a hypertext link with this URL | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-11142@lists.php.net to get a copy of this message | ||
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>
It sounds simple but actually it is pretty hard in some cases.
First problem: what are the characters that are "reserved" or
"unsafe" in an
URL? I found the following ones:
[:space:] ( ) < > # % { } [ ] | \ ^ :
Second problem: The URL is embedded in the text; imagine the following sentence
in the text:
just look at http://www.dbourdon.com. It's worth
visiting.
OK, apart the fact that it's really worth visiting :-> just after the URL
there
is a dot which is ponctuation and not part of the URL.
Third problem: it is to be done with EREGI_REPLACE and regular expressions, and
most of the signs are forbidden signs in regular expressions...
So here is whatr I find just before getting a headache:
$text = EREGI_REPLACE("(http://[^([:space:]\(\)<>#%:\{\}\|\\\^)]+[^([:space:]
\(\)<>#%:\{\}\|\\\^\.)])","<a
href=\"\\1\">\\1</a>",$text);
It does work but I think the regular expressions can be improved. I have tried
to read the RFCs about URL syntax but I must admit that it's impossible to
read...
Can you help me and improve the regular expression? I think some of you has
already dealt with this and it might be useful for lots of developers.
And sorry for my poor English...
Den.
--
Denis Bourdon - dbourdon@dbourdon.com
Etudiant à Telecom Grenoble ENSIMAG/ENSERG
site personnel: www.dbourdon.com
webmaster de l'espace étudiants de Telecom Grenoble:
www-telecoms.imag.fr/etudiants