Click to See Complete Forum and Search --> : Multiple email addresses and regular expressions


nemesis_256
04-06-2007, 07:01 PM
I'm trying to make a regular expression that will look through a string of text which has multiple email addresses (all just plain text). What I want it to do is to add the html anchor tag around the addresses. Here's what I have up to now.
ereg('[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+', $comment, $regs);
$comment = ereg_replace($regs[0], "<a href=\"mailto:$regs[0]\">$regs[0]</a>", $comment);
The first email address works fine, but after that it doesn't. So obviously, ereg only goes through until it finds the first match. Is this what it's supposed to do? I mean the results are saved in an array...

If I run through that code again it will just find the first email address, which this time will end up being the one between the anchor tag. So how do I make this work?

sae
04-06-2007, 09:46 PM
how do you have your loop setup?

NogDog
04-06-2007, 09:47 PM
I believe ereg() only finds the first match. But you don't really need to use it, just do the ereg_replace():

$comment = ereg_replace('[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+',
'<a href="mailto:\\0">\\0</a>', $comment);

nemesis_256
04-06-2007, 10:30 PM
So I assume \\0 is the "variable" with the string that was found? Thanks!

NogDog
04-07-2007, 12:39 AM
So I assume \\0 is the "variable" with the string that was found? Thanks!
Yes: \\0 is whatever matches the entire regex, \\1 is the first sub-pattern (if any), \\2 the 2nd sub-pattern, etc.