• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Change Function to Find Emails

I have a PHP function that finds tags in a variable, $string. I was wondering, how do I change this to find email addresses in $string? Note: Not all email addresses will be prescribed "mailto:" so it's probably a better approach to simply find all text before an "@" symbol that does not include any special characters (or a blank space). For example, from this ($string) the function should return as follows:
My name is John Doe and my email address is john@doe.net.

Returns:
john@doe.net
function findinside($start, $end, $string)
{
   preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)'. preg_quote($end, '/').'/i', $string, $m);
   return $m[1];
}

Open in new window

0
EMB01
Asked:
EMB01
  • 8
  • 4
  • 2
2 Solutions
 
Graceful_PenguinCommented:
Could you maybe give us an example string that will be passed to the function
0
 
thehagmanCommented:
0
 
EMB01Author Commented:
Thanks for the link, I'll check that out. I'm not that good with regex. From the question, an example of $string would be:
My name is John Doe and my email address is john@doe.net.

The function should return the email address:
john@doe.net
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
thehagmanCommented:
Then this should work (using the final regexp on the page mentioned - augmented by TLDs int, pro, asia, cat, coop, jobs, travel (arpa and tel are not supposed to be used for email)
$input = "My name is John Doe and my email address is john@doe.net.";
if (preg_match('/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|int|pro|asia|cat|coop|jobs|travel)\b/i', $input, $matches)) {
  echo $matches[0];
}

Open in new window

0
 
EMB01Author Commented:
At first, everything  past the following line was commented out:
if (preg_match('/[a-z0-9!#$%&'

Then, I escaped the apostrophe characters and ran with the code attached and I got this error:
Warning: preg_match() [function.preg-match]: Unknown modifier '=' in /var/www/vhosts/emarketbuilders.com/httpdocs/admin/get_data_test.php on line 39

What's causing this error?
$input = "My name is John Doe and my email address is john@doe.net.";
if (preg_match('/[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|int|pro|asia|cat|coop|jobs|travel)\b/i', $input, $matches)) {
  echo $matches[0];
}

Open in new window

0
 
thehagmanCommented:
Oops, sorry - I copied the regular expression from that page without perfroming the necessary quoting.
You noticed the apostrophe yourself - it must be qouted because the regex is contained in single quotes.
The other thing is that the regex is also contained in slashes - therefore the slash must be escaped as well: ... *+/=?... should become ...*+\/=?... in two places
0
 
EMB01Author Commented:
Could you post a working example code?
0
 
thehagmanCommented:
This time actually tested to output the right hing ;)
<?
$input = "My name is John Doe and my email address is john@doe.net.";
echo "input = $input\n";
if (preg_match('/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|int|pro|asia|cat|coop|jobs|travel)\b/i', $input, $matches)) {
  echo "output = ".$matches[0] ."\n";
}
 
?>

Open in new window

0
 
EMB01Author Commented:
Good to know! Hey, this may be a stupid question; but, if there is more than one match from $input, how do I echo them from the array?
0
 
Graceful_PenguinCommented:
You just run through it with a foreach.
<?
$input = "My name is John Doe and my email address is john@doe.net.";
echo "input = $input\n";
if (preg_match('/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|int|pro|asia|cat|coop|jobs|travel)\b/i', $input, $matches)) {
  foreach($matches as $match){
    echo "output = ".$match ."\n";
}
}
 
?>

Open in new window

0
 
EMB01Author Commented:
Yeah, thanks. Not sure where my mind was at yesterday...
0
 
EMB01Author Commented:
Thanks for your help. Have a great day.
0
 
EMB01Author Commented:
Hey, strange thing... I tried the attached code but it only echoes one match... What's up with that?
$input = "My name is John Doe and my email address is john@doe.net. My name is John Doe and my email address is johndoe@johndoe.net.";
echo "input = $input\n";
if (preg_match('/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|int|pro|asia|cat|coop|jobs|travel)\b/i', $input, $matches)) {
  foreach($matches as $match){
    echo "output = ".$match ."\n";
}
}

Open in new window

0
 
EMB01Author Commented:
Okay, I think I have to instead use the preg_match_all function, not the preg_match function.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now