Page 1 of 1

Mailto or URL, but not both!

Posted: Wed Jul 06, 2005 5:16 pm
by Todd_Z
I have a script that goes through a page and converts any valid urls to links starting with http:// and any valid email addresses to links to mailto:address. However, when I have something like email@site.com, it converts it both as a mailto and a url (site.com) This causes catastrophes. Does anyone have simple code for converting a conversation into hyperlinked text? Thanks!

Posted: Wed Jul 06, 2005 5:30 pm
by Chris Corbyn
I cant see your code but since you're a DIY kinda person (I think :?) I'll give you a pointer to try playing with.

Neagtive Lookbehinds could help here (checking for @).

They work like this (this is very pseudo) ...

Code: Select all

/(?<!foo)bar/

Code: Select all

/(?<!@)site.com/
The opposite (not needed here) is positive lookbehinds

Code: Select all

/(?<=foo)bar/
One downside to Lookbehinds is that they are fixed width. In other words, only a pattern with a fixed length can be used inside the (?<! ) group. (?<!\w{2,5}) is NOT allowed for example, but (?<=\w{5}) is.

Posted: Wed Jul 06, 2005 5:53 pm
by Burrito
something fishy is awry here.

wouldn't be for the chat thingy would it? :wink:

Posted: Wed Jul 06, 2005 10:40 pm
by Todd_Z
You caught me burrito - but you did it js wise, im rockin the php scriptin. Thats for the help d11

Posted: Wed Jul 06, 2005 10:43 pm
by Burrito
great that is!

send me your changes you must :P

Posted: Thu Jul 07, 2005 9:08 pm
by Todd_Z

Code: Select all

$msg = preg_replace( "#(?<!@)(?:http://)?((?:www\.)?\w+\.\w+)#i", "<a href=\"http://$1\">$1</a>", $msg );
Problem: name@gmail.com
returns name@g<a href=\"http://mail.com\">mail.com</a>

Code: Select all

$msg = preg_replace( "#([a-z0-9_.-]+@[a-z0-9-]+.[a-z0-9-.]+)#i", "<a href=\"mailto:$1\">$1</a>", $msg );
^^^ Seems to work well.