/** * Parse the from field into it's components - email and name * @param $string * @param $default_address * @return array with fields 'email' and 'name' */ public function parseEmailAndName($string, $default_address) { if (preg_match("/([^ ]+@[^ ]+)/", $string, $regs)) { # if there is an email in the from, rewrite it as "name <email>" $name = str_replace($regs[0], "", $string); $email_address = $regs[0]; # if the email has < and > take them out here $email_address = str_replace('<', '', $email_address); $email_address = str_replace('>', '', $email_address); # make sure there are no quotes around the name $name = str_replace('"', '', ltrim(rtrim($name))); } elseif (strpos($string, ' ')) { # if there is a space, we need to add the email $name = $string; $email_address = $default_address; } else { $email_address = $default_address; $name = $string; } return ['email' => $email_address, 'name' => String::removeDoubleSpaces(trim($name))]; }