Welcome Guest, Not a member yet? Register   Sign In
My problem with separate emails
#1

[eluser]frrose[/eluser]
Hello every body

i have this code to separate emails from srt
Code:
function get_emails ($str)
        {
           $emails = array();
           preg_match_all("/\b\w+\@\w+[\.\w+]+\b/", $str, $output);
           foreach($output[0] as $email) array_push ($emails, strtolower($email));
           if (count ($emails) >= 1) return $emails;
           else return false;
        }
but my problem
if there is something like aaa@aaa the code will take it like email Sad
also if there is an email like [email protected] willtake just [email protected] Sad

is there any body can help me ??
#2

[eluser]sophistry[/eluser]
email address regex is very complicated. see this post for some work i did about 6 months ago.

http://ellislab.com/forums/viewthread/89516/
#3

[eluser]frrose[/eluser]
I saw your code there and made this change on my code

Code:
function get_emails ($str)
        {
           $emails = array();
           preg_match_all("/([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $str, $output);
           foreach($output[0] as $email) array_push ($emails, strtolower($email));
           if (count ($emails) >= 1) return $emails;
           else return false;
        }

but i also still have a problem

if on str i have [email protected]@bbb.bbb

the code will take just [email protected] Sad

last part of email must have one . only

how to do it ?

thanks
#4

[eluser]Mike Ryan[/eluser]
Validating email addresses is more complicate than you might think - see here for a more detailed explanation.

The last part of an email address can definitely have more than one dot - e.g. [email protected] is a valid email address.

I would split the array based on your chosen delimiter, then test each email address. Let's say your delimiter is the space character:

Code:
$addresses = explode(' ', $emails );
foreach ($addresses as $address)
{
  if (is_valid_email($address))
  {
    array_push($valid_emails, $address)
  }
  else
  {
    array_push($invalid_emails, $address)
  }
}
#5

[eluser]sophistry[/eluser]
@ffrose... that's not my code. my code is much more complex and inscrutable! ;-)

also, what exactly is wrong with that behavior you cite anyway? what would you like the email detector to do? it has to make some kind of decision because there is only one @ sign allowed in an email address (assuming you aren't parsing the more free-form section and just dealing with actual addresses).




Theme © iAndrew 2016 - Forum software by © MyBB