parse text for URLs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
docstevie
Forum Newbie
Posts: 3
Joined: Tue Jul 23, 2002 4:15 pm

parse text for URLs

Post 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.
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post 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?
docstevie
Forum Newbie
Posts: 3
Joined: Tue Jul 23, 2002 4:15 pm

Post 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.
User avatar
QWERTY
Forum Newbie
Posts: 20
Joined: Sat Jun 29, 2002 10:57 am
Location: Slovenia

...

Post 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
User avatar
musashi
Forum Commoner
Posts: 39
Joined: Tue Jul 23, 2002 12:51 pm
Location: Santa Cruz - CA

Reg expression

Post 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!
docstevie
Forum Newbie
Posts: 3
Joined: Tue Jul 23, 2002 4:15 pm

Post by docstevie »

thanx a lot!!! :D
Post Reply