Quick Regex question

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
tomerg3
Forum Newbie
Posts: 4
Joined: Wed Jul 18, 2007 11:45 am

Quick Regex question

Post 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.
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

Post 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!
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Code: Select all

~(?>[\w.-]+@[\w.-]+)(?!</a>)~
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.
tomerg3
Forum Newbie
Posts: 4
Joined: Wed Jul 18, 2007 11:45 am

Post 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 :(
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

Post 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!
tomerg3
Forum Newbie
Posts: 4
Joined: Wed Jul 18, 2007 11:45 am

Post by tomerg3 »

stereofrog wrote:

Code: Select all

~(?>[\w.-]+@[\w.-]+)(?!</a>)~
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...
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

you forgot the delimiters ~
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You forgot the delimiters.
tomerg3
Forum Newbie
Posts: 4
Joined: Wed Jul 18, 2007 11:45 am

Post by tomerg3 »

stereofrog wrote:you forgot the delimiters ~
I didn't forget them, I took them out on purpose 8O, 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!
Post Reply