Hi folks,
I'm looking for a regular expression which matches a URL. I've found
several already by looking around various sites, but they don't seem
to accomplish what I'm looking for.
I'm developing a PHP-driven message board / forum system. As a part
of this, of course, I have to give users the ability to put links
in their messages without allowing them to post HTML, so I've settled
on the markup "[link=URL]text[/link]" (odd how much that looks like
an <A> tag, I know). There is also an [image=URL] tag which gets
changed into an <IMG> tag. Additionally, for user-friendliness, I'd
like to catch any URLs that are in the body of the text themselves -
these will be preceded by a newline, a space or will be at the very
start of the message, so the same RegEx should suffice.
The RegEx's I've tried so far:
1. (From the online PHP manual's user-contributed notes):
[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]
Works ok for picking out URLs from plain text, but doesn't work with
[link=URL]text[link] ... it seems to eat the closing ] of the [link]
markup and it's all bad from there on out. Works fine though if I
change the markup rule to [link=URL<space>]text[/link], but that's
nonstandard and not very intuitive.
2. (From regexlib.com, long line sorry)
(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a
-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]
Works fine for [link=URL]text[link] and picking URL itself out of
plain text, as long as URL isn't too complicated. It's happy with
a URL like http://www.shadow.net.au but when that URL has a path
after it, like http://www.game-spin.com/forums/gfx/stars/star1.gif
then it gets real useless, real fast.
If anyone's interested, the board system itself is up and running
on my dev/test server at http://boards.shadow.net.au - you can see
a live version at http://www.auswrestling.com/forum/ and also at
http://www.game-spin.com/forums/. I'll be releasing it soon, and
if someone can give me a working URL RegEx I'd be happy to give
them some credits in the documentation ;)
Thanks in advance...
Jason
--
Jason Murray
[EMAIL PROTECTED]
Web Design Team, Melbourne IT
Fetch the comfy chair!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php