Page 1 of 1

Need Help with filter_var()

Posted: Wed Aug 29, 2012 12:46 pm
by Pavilion
Hello:

I am trying to extract email addresses from a larger string. Following is my current attempt:

Code: Select all

			foreach(preg_split('/ /', $string) as $token) {
				echo "Token: " . $token ."<br />";
				$email = filter_var(filter_var($token, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);			
				$AdminAddress = stripos($email, "admin@mysite");
				if ($AdminAddress !==0 && $email !== false) {
				echo "EMAIL: " . $email . "<br />";
					$emails[] = $email;
				}
						//return $emails;			
			}
For the most part everything is working fine. But with one email content the filter_var() is not functioning properly. Specifically:
  1. The Token echo is returning: DDuck@yahoo.com Subject: Notice the space between .com and Subject
  2. After the $token is run through filter_var() it returns the following: JAminson@yahoo.comSubject. Subject is now added to the email address.
Does anyone have any idea how to handle this. Most of the email addresses are extracting just fine, but there are a few that come back with another word appended to the end of the address. Any help is appreciated.

Thanks Much:

Pavilion

Re: Need Help with filter_var()

Posted: Wed Aug 29, 2012 1:14 pm
by Benjamin
Are you sure it's not a TAB?

Re: Need Help with filter_var()

Posted: Thu Aug 30, 2012 6:06 am
by Pavilion
Benjamin wrote:Are you sure it's not a TAB?
I figured it out. My preg_split is now:

Code: Select all

foreach(preg_split("/[\s,]+/", $content) as $token)
Thanks for your input Benjamin - regex just drives me nuts. I don't understand the logic and have to look for examples on the internet. A cumbersome approach, I know. Hopefully, the more I use regex, the better I'll get at it.

Pavilion