Home > PHP, PHP Code Examples, Programming > PHP Script to Extract Email Address from any text

PHP Script to Extract Email Address from any text

November 14th, 2007 admin Leave a comment Go to comments

I have developed a function that you can embed in your PHP applications that will help you extract email addresses from a given piece of text.

I have tested this on a string of (actually 4 – 5 paragraphs) text and this has performed very well.

Please feel free to use this code in your applications and let me know if you face any issues.




 
$text = ' [email protected] ';
 
function parseTextForEmail($text) {
	$email = array();
	$invalid_email = array();
 
	$text = ereg_replace("[^A-Za-z._0-9@ ]"," ",$text);
 
	$token = trim(strtok($text, " "));
 
	while($token !== "") {
 
		if(strpos($token, "@") !== false) {
 
			$token = ereg_replace("[^A-Za-z._0-9@]","", $token);
 
			//checking to see if this is a valid email address
			if(is_valid_email($email) !== true) {
				$email[] = strtolower($token);
			}
			else {
				$invalid_email[] = strtolower($token);
			}
		}
 
		$token = trim(strtok(" "));
	}
 
	$email = array_unique($email);
	$invalid_email = array_unique($invalid_email);
 
	return array("valid_email"=>$email, "invalid_email" => $invalid_email);
 
}
 
function is_valid_email($email) {
	if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$",$email)) return true;
	else return false;
}
 
var_dump(parseTextForEmail($text));

In the above program, you should pass a string of text to the method parseTextForEmail() that will return an array with valid and invalid email addresses.

Check the code and let me know if you have any issues or suggestions for improvements.

You can also check my PHP Tutorials – Starting with Creating a PHP5 Class




Categories: PHP, PHP Code Examples, Programming Tags:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/themes/inove/templates/header.php:29) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/themes/inove/templates/header.php:29) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. November 17th, 2007 at 08:09 | #1

    Very bloated way of pulling an email address from a piece of text.
    An alternative would be preg_match,

    preg_match_all(‘/([\w\d\.\-\_]+)@([\w\d\.\_\-]+)/mi’, $string, $matches);
    var_dump($matches);

    Regex was done very quickly, but check out the results.
    Verify the results using your verify func if you need to =]

  2. rupal
    January 24th, 2008 at 18:25 | #2

    Hi,your code works fine.Thankyou for giving such a nice function.

  3. Will
    February 24th, 2008 at 07:19 | #3

    Thank you very much – works great for me.

    It would be nice to have one to extract all url too!

  4. amit
    April 15th, 2008 at 13:21 | #4

    when i tested this script of urs
    named extracting email from string or text
    this script raise an notice

    Notice: Array to string conversion in G:\Program Files\EasyPHP 2.0b1\home\Demo\mailchk.php on line 38
    array(2) { ["valid_email"]=> array(1) { [0]=> string(16) “[email protected]” } ["invalid_email"]=> array(0) { } }

    could u explain it me

    thanks
    amit

  5. November 1st, 2009 at 17:08 | #5

    Its really awesome. I was looking for such script and I got this thanks

  6. February 17th, 2010 at 20:33 | #6

    thanks!

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )