[ create a new paste ] login | about

Link: http://codepad.org/Aao5phc2    [ raw code | output | fork ]

PHP, pasted on May 22:
<?
	// validate email address
	function validate_email( $email ){
		return (filter_var($email, FILTER_VALIDATE_EMAIL)) ? true : false;
	}

	// split email into name / address
	function email_split( $str ){
		$name = $email = '';
		if (substr($str,0,1)=='<') {
			// first character = <
			$email = str_replace( array('<','>'), '', $str );
		} else if (strpos($str,' <') !== false) {
			// possibly = name <email>
			list($name,$email) = explode(' <',$str);
			$email = str_replace('>','',$email);
			if (!validate_email($email)) $email = '';
			$name = str_replace(array('"',"'"),'',$name);
		} else if (validate_email($str)) {
			// just the email
			$email = $str;
		} else {
			// unknown
			$name = $str;
		}
		return array( 'name'=>trim($name), 'email'=>trim($email) );
	}
	
	// test it
	$tests = array(
		'person@place.com',
		'monarch <themonarch@tgoci.com>',
		'blahblah',
		"'doc venture' <doc@venture.com>"
		);
	
	foreach ($tests as $test){
		echo print_r( email_split($test), true );
	}

?>


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Array
(
    [name] => 
    [email] => person@place.com
)
Array
(
    [name] => monarch
    [email] => themonarch@tgoci.com
)
Array
(
    [name] => blahblah
    [email] => 
)
Array
(
    [name] => doc venture
    [email] => doc@venture.com
)


Create a new paste based on this one


Comments: