Page 1 of 1
Quick Regex question
Posted: Wed Jul 18, 2007 11:52 am
by tomerg3
This is a php Regex, but I have a feeling I can find help here...
I am using this code to convert a
email@email.com (within a larger text) to <a href="mailto: ..">
I want to modify it so if there's already a <a href="mailto:...> in that text, it will not change it
My current code is:
Code: Select all
$text = eregi_replace("([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})","><a href=\"mailto:\\1\">\\1</a>", $text);
Help please.
Posted: Wed Jul 18, 2007 1:07 pm
by ReverendDexter
why not split it up into two lines, check for the <mailto:... in a pregmatch, and if it's not there, then add it?
Psudeocode would be like:
Code: Select all
if (!pregmatch('/<mailto:/', $address))
$address = '<mailto:' . $address . '>';
Like I said, just psuedocode, but hopefully it gets you started!
Posted: Wed Jul 18, 2007 1:11 pm
by stereofrog
this will match an email address only if it's not followed by </a> closing tag.
you need preg_replace not ereg for this to work.
Posted: Wed Jul 18, 2007 1:30 pm
by tomerg3
ReverendDexter wrote:why not split it up into two lines, check for the <mailto:... in a pregmatch, and if it's not there, then add it?
Psudeocode would be like:
Code: Select all
if (!pregmatch('/<mailto:/', $address))
$address = '<mailto:' . $address . '>';
Like I said, just psuedocode, but hopefully it gets you started!
My string will have both regular emails and active link emails.
I can do a split on <a> tags but that will be very ugly, it was much easier doing the same thing for http:// links, but the email doesn't seem to work for me

Posted: Wed Jul 18, 2007 1:39 pm
by ReverendDexter
tomerg3 wrote:My string will have both regular emails and active link emails.
Wait... you mean that you're looking at a single long string that could have either or both?
What about looking for whitespaces around the
xxx@yyyy.zzz? I mean, if it's in a tag, it shouldn't have whitespaces on either side, so you could just modify your preg_replace (or s///) to look for that...
Again, just a thought, hope it helps!
Posted: Wed Jul 18, 2007 1:52 pm
by tomerg3
stereofrog wrote:
this will match an email address only if it's not followed by </a> closing tag.
you need preg_replace not ereg for this to work.
I tried
Code: Select all
$text = preg_replace("(?>[\w.-]+@[\w.-]+)(?!</a>)","Test", $text);
but it makes $text empty...
Posted: Wed Jul 18, 2007 1:59 pm
by stereofrog
you forgot the delimiters ~
Posted: Wed Jul 18, 2007 2:00 pm
by superdezign
You forgot the delimiters.
Posted: Wed Jul 18, 2007 2:45 pm
by tomerg3
stereofrog wrote:you forgot the delimiters ~
I didn't forget them,
I took them out on purpose 
, it's been a while since I did a preg....
Anyway it almost worked, the new problem was that it was trying to replace the replaced string as well.
Since now it added
Code: Select all
<a href="mailto:a@b.com">a@b.com</a>
it tried to replace the
a@b.com"> with another "a href" (since it doesn't end with </a>.
I tried
Code: Select all
~(?>[\w.-]+@[\w.-]+)(?!</a>)[B](?!">)[/B]~
It works great now, thanks a lot!