Page 1 of 1

parse text for URLs

Posted: Tue Jul 23, 2002 4:15 pm
by docstevie
hi,

i need to find all URLs in a text like a guestbook-entry, so the URLs are not formatted in a special way, like they might begin with http:// or just with www. the harder part is the ending: URLs might end with the end of the whole text, with a newline character, with a /, with the domain name...

i think using regular expressions is the right way to convert URLs to hyperlinks, but since i'm not experienced at all with regular expressions, it's quite hard.

since this is a very usual problem, i'm sure you can help me finding the right regular expression. - at least i hope so ;-)

thanks in advance, stevie.

Posted: Tue Jul 23, 2002 4:20 pm
by RandomEngy
Why do I have the feeling "helping you find the right expression" would involve someone giving you the exact expression to copy/paste into PHP?

Posted: Tue Jul 23, 2002 4:29 pm
by docstevie
well, as i said, i think it's a common problem, so someone might already have the solution without having to spend hardly any time just posting it.

...

Posted: Tue Jul 23, 2002 5:50 pm
by QWERTY
Code:

Code: Select all

//FOR URL
$text = preg_replace ("/(ftp|http|https):\/\/(&#1111;a-z0-9~#%@&:;=!',_crl\(\)\?\/\.\-\+\&#1111;\]\|\*\$\^\&#123;\&#125;]+)/i", "<a href="\\1://\\2" target="_blank">\\1://\\2</a>", $text); 
//FOR EMAIL LINKS
$text = preg_replace ("/(&#1111;\s|"])(&#1111;\w|\.|\-|_]+)@(&#1111;\w||\-|_]+)\.(&#1111;\w|\.|\-|_]+)/i", "\\1<a href="mailto:\\2@\\3.\\4">\\2@\\3.\\4</a>", $text);
You'll fint tutorial here:
http://www.evolt.org/article/rating/20/22700/index.html

Anything else? :D

Reg expression

Posted: Tue Jul 23, 2002 6:11 pm
by musashi
Try this:

Code: Select all

preg_match_all("(&#1111;http:\/\/]*www\.&#1111;^\s]+)",$text,$out);
This only thing that is odd is I tried the ? (for 0 or 1) character instead of the * (0 or more) and it didn't work. This version works quite well. The only failing point would be if the user types: http://http://www.blah.com it would match the whole thing. Since that isn't likely, I think this is a good step. So the results will be in the $out variable, in the first index.

Thus: $out[0] will contain an array of all of the matching strings.

Hope that helps!

Posted: Tue Jul 23, 2002 6:44 pm
by docstevie
thanx a lot!!! :D