PHPMailer\PHPMailer
PHPMailer::parseEmailString
Parse a string containing an email address with an optional name and divide it into a name and email address.
Method of the class: PHPMailer{}
No Hooks.
Returns
Array{name:. string, email: string}
Usage
$result = PHPMailer::parseEmailString( $input );
- $input(string) (required)
- The email with name.
PHPMailer::parseEmailString() PHPMailer::parseEmailString code WP 6.9
private static function parseEmailString($input)
{
$input = trim((string)$input);
if ($input === '') {
return ['name' => '', 'email' => ''];
}
$pattern = '/^\s*(?:(?:"([^"]*)"|\'([^\']*)\'|([^<]*?))\s*)?<\s*([^>]+)\s*>\s*$/';
if (preg_match($pattern, $input, $matches)) {
$name = '';
// Double quotes including special scenarios.
if (isset($matches[1]) && $matches[1] !== '') {
$name = $matches[1];
// Single quotes including special scenarios.
} elseif (isset($matches[2]) && $matches[2] !== '') {
$name = $matches[2];
// Simplest scenario, name and email are in the format "Name <email>".
} elseif (isset($matches[3])) {
$name = trim($matches[3]);
}
return ['name' => $name, 'email' => trim($matches[4])];
}
return ['name' => '', 'email' => $input];
}