0

I am using oauth for yahoo api to import email contact. And I have been successfully importing all my contact email from yahoo into a page.

My problem now is I am using a javascript to highlight each email in the text area.

This is the sample of the JavaScript which will highlight each email.

   echo "$(\"#email-tags\").addTag(\"yahoo@yahoo.com\");";

Now if there is something which is not formatted as email, the text area will be blank won't show any email.

My question now is how to import only something which is formatted as email. Because sometime in email contact, there is some contact from mailer which its format is like this: DAFDGREGSDFHASRFEW2 <= Not email format, which will make my javascript error in the text area and won't show any email.

Here is some last line of yahoo email contact:

// Call Contact API 
$retarrs = callcontact_yahoo(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $guid, $access_token, $access_token_secret, false, true);

//I have to use a delimiter of white space to separate each email here.  
$ymail = str_replace(',', ' ', $retarrs); 

//This is my javascript tag & the email contact list will show.     
echo "$(\"#email-tags\").addTag(\"".$ymail."\");";
alisa
  • 259
  • 3
  • 15

1 Answers1

0

You can use this method from this post:

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // invalid emailaddress
}

So, your final might look like (in PHP):

    function is_email($email){
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        return true;
    }
    else
        return false;
}

// Call Contact API
$retarrs = "test@test.com,peter@peter.com,hey@@hey,nothing";

//Explode, check, and re-collapse the string
$exploded = explode(",", $retarrs);
$exploded_emails = array();
foreach ($exploded as &$string) {
    if (is_email($string))
        $exploded_emails[] = $string;
}
$ymail = implode(" ",$exploded_emails);

echo "the emails are:" . $ymail;
Community
  • 1
  • 1
PeterM
  • 439
  • 2
  • 9
  • It doesn't show any email now. It only show the email address if only I echo the $retarrs. – alisa Feb 02 '15 at 12:56
  • Hey alisa. Sorry, I made two typos. The above is tested and works. – PeterM Feb 02 '15 at 13:47
  • the problem is in the $retarrs the delimiter is not white-space, but comma. it would be like `$retarrs = "test@test.com,peter@peter.com,hey@@hey,nothing";` – alisa Feb 02 '15 at 16:58
  • And you want the output to be with spaces? I changed it - see above. If you want the output to be with commas or something else, just change what's between the quotes in $ymail = implode(" ",$exploded_emails); – PeterM Feb 04 '15 at 06:52