SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Count each email in a text box/input field

    I have a text box and I'm trying to get the number of actual email addresses that are in it. Problem is the format. Sometimes the input box has:

    "john smith" <jonnyboy@gmail.com>

    sometimes it's

    "jonnyboy@gmail.com" <jonnyboy@gmail.com>.

    sometimes it's just the email address itself "jonnyboy@gmail.com",

    I have a function to strip out the < > and " " but how do I strip it out of each one? Here's the code:
    PHP Code:

    echo  $firstpart;  // shows Heidi Klum Supermodel
    echo "<br>";
    echo  
    $secondpart;  // shows info@HeidiKlum.com
       
    $firstpart extractBetweenDelimeters($Recipients,"\"","\"");
    $secondpart extractBetweenDelimeters($Recipients,"<",">");   
       
       function 
    extractBetweenDelimeters($inputstr,$delimeterLeft,$delimeterRight) {
        
    $posLeft  stripos($inputstr,$delimeterLeft)+1//posLeft is returning a number for the character that's the left of <
        
    $posRight stripos($inputstr,$delimeterRight,$posLeft+1); //posRight is >    
        
    $each substr($inputstr,$posLeft,$posRight-$posLeft);
        return 
    $each;
       } 
    A likely string of emails would be:
    "Bobeee Bob" <Bob@gmail.com>, "Ron@hotmail.com" <Ron@hotmail.com>, "johnee@ruckus.com" <johnee@ruckus.com>

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why not just look for something that looks like a valid email address?

    PHP Code:
    $textstring '"Bobeee Bob" <Bob@gmail.com>, "Ron@hotmail.com" <Ron@hotmail.com>, "johnee@ruckus.com" <johnee@ruckus.com>';
    $emails explode(',',$textstring);
    foreach (
    $emails as $email)
    {
        
    preg_match('/[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+(\.[a-zA-Z]{2,5})+/'$email$match);
        if (
    $match[0])
        
    $email_list[] = $match[0];
    }
    print_r($email_list); 

  3. #3
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're the man...Thanks! That worked great. here's the code I used for anyone that's interested. I just added a count to figure out how many I had.

    PHP Code:
    $Recipients $_REQUEST['Recipients'] ;      

            
    //Count emails:
            
    $emails explode(',',$Recipients);
            
            foreach (
    $emails as $email){    
                
    preg_match('/[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+(\.[a-zA-Z]{2,5})+/'$email$match);
                if (
    $match[0]) 
                
    $email_list[] = $match[0];
            }
            
    //print_r($email_list); 
            
            
    $result count($email_list);
            
    //echo $result;    
            
            //end Count emails 
    Thanks again!

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If all you're doing is counting, no need to store it into an array?

    Just do

    if ($match[0]) $emailcount++;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •