Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
tomerg3
Forum Newbie
Posts: 4 Joined: Wed Jul 18, 2007 11:45 am
Post
by tomerg3 » Wed Jul 18, 2007 11:52 am
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.
ReverendDexter
Forum Contributor
Posts: 193 Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA
Post
by ReverendDexter » Wed Jul 18, 2007 1:07 pm
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!
stereofrog
Forum Contributor
Posts: 386 Joined: Mon Dec 04, 2006 6:10 am
Post
by stereofrog » Wed Jul 18, 2007 1:11 pm
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 » Wed Jul 18, 2007 1:30 pm
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
ReverendDexter
Forum Contributor
Posts: 193 Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA
Post
by ReverendDexter » Wed Jul 18, 2007 1:39 pm
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 » Wed Jul 18, 2007 1:52 pm
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...
stereofrog
Forum Contributor
Posts: 386 Joined: Mon Dec 04, 2006 6:10 am
Post
by stereofrog » Wed Jul 18, 2007 1:59 pm
you forgot the delimiters ~
tomerg3
Forum Newbie
Posts: 4 Joined: Wed Jul 18, 2007 11:45 am
Post
by tomerg3 » Wed Jul 18, 2007 2:45 pm
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!