Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.