We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Change Function to Find Emails

EMB01
EMB01 asked
on
Medium Priority
217 Views
Last Modified: 2012-05-06
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

Comment
Watch Question

Could you maybe give us an example string that will be passed to the function

Author

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
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

Author

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

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

Author

Commented:
Could you post a working example code?
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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?
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

Author

Commented:
Yeah, thanks. Not sure where my mind was at yesterday...

Author

Commented:
Thanks for your help. Have a great day.

Author

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

Author

Commented:
Okay, I think I have to instead use the preg_match_all function, not the preg_match function.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.