PDA

Click to See Complete Forum and Search --> : regular expression of links.


yair24
Jan 22nd, 2004, 03:32 AM
hello,
I am building a system that allows users to enter text.
something like a guestbook.

I would like to do that if the user write a link that starts with www in the text it will turn the link into a clickable link that means it will add the <a href=""></a> tag to it.

is there any regular expression that can help me with such thing?
thanks in advanced for any help.

Yair

CornedBee
Jan 22nd, 2004, 05:23 AM
This expression should do the same as the link finder of vBulletin.

"(www\.[^\w]+)|(http://[^\w]+)"

You need to add the http:// if it isn't already there, else the link is interpreted as relative.

yair24
Jan 22nd, 2004, 07:05 AM
I will now try to find a place which will help me to understand this regular expression.

thanks again

Yair

CornedBee
Jan 22nd, 2004, 09:39 AM
Err... ask me? ;)

The expression matches one of two patterns. The first pattern is
(www\.[^\w]+)
Which means "www." followed by 1 or more non-whitespace characters. Captured.

The second pattern is
(http://[^\w]+)
Which mean "http://" followed by 1 or more non-whitespace characters. Captured.

yair24
Jan 22nd, 2004, 10:48 AM
I didnt wanted to bother you with lazyness questions ...

:)

thank you
Yair

CornedBee
Jan 22nd, 2004, 12:12 PM
Everyone else does...

The Hobo
Jan 22nd, 2004, 06:52 PM
CornedBee, I got an error (Unknown modifier '|') when I tried that. :confused:

I've always used:

$text = preg_replace('/(?<!<a href=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i', '<a href="\\0">\\0</a>', $text);

CornedBee
Jan 23rd, 2004, 05:16 AM
'tis weird, | is one of the most basic features of regular expressions.

The Hobo
Jan 23rd, 2004, 11:55 AM
Originally posted by CornedBee
'tis weird, | is one of the most basic features of regular expressions.

Does the expression work for you?

I tried changing it to:

"((www\.[^\w]+)|(http://[^\w]+))"

Thinking that might help. It gets rid of the error, but doesn't do anything to the text.

CornedBee
Jan 23rd, 2004, 12:03 PM
Haven't tried. I just think it's weird that I get such a simple expression wrong ;)