Jump to content

Script to catch email addresses in paragraph.


salman_ahad@yahoo.com

Recommended Posts

 

I have a pragraph full of email addresses. I need to catch emails only and ignore the rest.

 

if ($_POST['Submit'])
{
$para = $_POST['para'];
        $emails = split('[<>;:,\/"{}*!?]',$para);	
        foreach ($emails as $email)
           {
        	if (empty($email)) {
                continue;} //I need help here for deleting those empty rows.
       else {
               // echo $email.'<br \>';
               // or insert in database.....
............
                     }
            }
}

 

Also I need help applying this email validation ereg, guide me here too please

 

$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";

//or

'/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/'

//or Anything better before inserting in database??

 

Yes I think it should be in regex, but I dont know how ??

 

My updated code

 

if ($_POST['Submit'])
{
$para = $_POST['para'];
        $emails = split('[<>;:,\/"{}*!?]',$para);	
foreach ($emails as $email)
	{
	if (empty($email)) {
        	continue;}
	if(stristr($email, '@') === FALSE) {  //Checking for those strings containing '@'
	continue;}
	else {
			echo $email.'<br \>';
		}
	}
}

 

My output is almost there...but I am also getting strings like

 


[email protected] ---------- Forwarded message ---------- From

//in these type of strings I think need to use regex... any help

 

Now this is my updated code to take junk of data and catch only email addresses.

 

<?php
if ($_POST['Submit'])
{
$para = $_POST['para'];
$emails = preg_split( '@[ <>;:,\\\\/"{}*!?]@', $para ); //split the para when there are signs encountered, excluding '@', '.', '_'
foreach ($emails as $email)
	{
	if (empty($email)) {
    	continue;}
	if(stristr($email, '@') === FALSE) {
	continue;}
       	else {
		echo $email.'<br \>';
		continue;
		}
	}
}
//$regexp = '/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/';
?>

 

Now this is my input data

 


[email protected]

---- Forwarded Message ----
From: Sattar <[email protected]>
To: [email protected]
Sent: Tue, November 17, 2009 9:23:57 AM

 

My output is like

 


[email protected] -----             // notice '-----' at the end, I need to omit that.
[email protected]
[email protected] Sent                    //notice 'Sent' at the end, I need to omit that.

//Dont know how to modify my code more to remove those

Yeah, like salathe says, something simple like

 

<?php
preg_match_all('~\b[^@\s<>]+@[^@\s<>]+~', $_POST['para'], $matches);
echo '<pre>' . print_r($matches[0], true) . '</pre>';
?>

 

should do the job fine, if you don't care about validating the emails.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.