Mailto or URL, but not both!
Moderator: General Moderators
Mailto or URL, but not both!
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!
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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) ...
The opposite (not needed here) is positive lookbehinds
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.
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/Code: Select all
/(?<=foo)bar/Code: Select all
$msg = preg_replace( "#(?<!@)(?:http://)?((?:www\.)?\w+\.\w+)#i", "<a href=\"http://$1\">$1</a>", $msg );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 );