从 PHP 中的完整电子邮件地址解析名称
Parse name from full email address in PHP
我想从完整的电子邮件地址中获取姓名,并尝试了 htmlentities 和 html_entity_decode,但特殊字符 <
和 >
导致无法获取姓名。由于完整的电子邮件地址来自数组(键值)而不是字符串。
数组值:
Array
(
[ENV_MAIL] => "Project Bribara<project.bribara@gmail.com";
)
尝试过的代码:
// Project email
$environment['ENV_MAIL'] = "Project Bribara<project.bribara@gmail.com";
$projectEmail = $environment['ENV_MAIL'];
echo $projectEmail ;
$projectName = explode("@", htmlentities($adminEmail));
echo html_entity_decode($projectName);
预计:
"Project Bribara"
实际:
"Project Bribara<project.bribara
如何获得预期的输出?
您应该使用“<" 首先。然后 split 与 "@" 如果你愿意的话。
你可以这样做。这里我们使用 explode()
函数来拆分字符串。
$environment['ENV_MAIL'] ="Project Bribara<project.bribara@gmail.com";
echo explode("<",$environment['ENV_MAIL'])[0];
您可以使用 Substr 和 strpos。
Substr 将字符串从第一个字符拆分到 strpos 找到 <
.
的位置
$str = "Project Bribara<project.bribara@gmail.com";
// Or ^^ is $projectEmail if I understand it correct?
Echo substr($str, 0, strpos($str , "<"));
如果 <
导致问题,也许您可以尝试正则表达式?
https://regex101.com/r/EfkwLX/1
// Match a-Z and space multiple until non word character
$re = '/([a-zA-Z ]+)\W/';
$str = 'Project Bribara<project.bribara@gmail.com';
preg_match($re, $str, $matches);
Echo $matches[1];
// Project email
$environment['ENV_MAIL'] = "Project Bribara<project.bribara@gmail.com";
$projectEmail = $environment['ENV_MAIL'];
echo $projectEmail;
$projectName = html_entity_decode ($projectEmail);
echo $projectName;
有几种方法可以解决这个问题:(Demo Link)
所有其他答案均失败的原因是您错误地陈述了输入字符串。您的实际输入字符串包含 <
,它是 <
的 html 实体。我下面的方法将向未来的 SO 读者阐明这一点,并帮助您 select 为您的项目找到最佳方法。
$environment['ENV_MAIL']="Project Bribara<project.bribara@gmail.com";
// ^^^^ html entity causing you trouble
$decoded_string=html_entity_decode($environment['ENV_MAIL']); // convert "<" to "<"
var_export(explode('<',$decoded_string)); // generate an array of the two parts for future access
echo "\n\n";
echo "Project Name = ",strstr($decoded_string,'<',true); // use strstr on < of decoded string with "true" param
echo "\n\n";
echo "Project Email = ",substr($decoded_string,strpos($decoded_string,'<')+1); // use substr&strpos on <
echo "\n\n";
// or less potentially damaging to your input string...
var_export(explode('<',$environment['ENV_MAIL']));
输出:
array (
0 => 'Project Bribara',
1 => 'project.bribara@gmail.com',
)
Project Name = Project Bribara
Project Email = project.bribara@gmail.com
array (
0 => 'Project Bribara',
1 => 'project.bribara@gmail.com',
)
我想从完整的电子邮件地址中获取姓名,并尝试了 htmlentities 和 html_entity_decode,但特殊字符 <
和 >
导致无法获取姓名。由于完整的电子邮件地址来自数组(键值)而不是字符串。
数组值:
Array
(
[ENV_MAIL] => "Project Bribara<project.bribara@gmail.com";
)
尝试过的代码:
// Project email
$environment['ENV_MAIL'] = "Project Bribara<project.bribara@gmail.com";
$projectEmail = $environment['ENV_MAIL'];
echo $projectEmail ;
$projectName = explode("@", htmlentities($adminEmail));
echo html_entity_decode($projectName);
预计:
"Project Bribara"
实际:
"Project Bribara<project.bribara
如何获得预期的输出?
您应该使用“<" 首先。然后 split 与 "@" 如果你愿意的话。
你可以这样做。这里我们使用 explode()
函数来拆分字符串。
$environment['ENV_MAIL'] ="Project Bribara<project.bribara@gmail.com";
echo explode("<",$environment['ENV_MAIL'])[0];
您可以使用 Substr 和 strpos。
Substr 将字符串从第一个字符拆分到 strpos 找到 <
.
$str = "Project Bribara<project.bribara@gmail.com";
// Or ^^ is $projectEmail if I understand it correct?
Echo substr($str, 0, strpos($str , "<"));
如果 <
导致问题,也许您可以尝试正则表达式?
https://regex101.com/r/EfkwLX/1
// Match a-Z and space multiple until non word character
$re = '/([a-zA-Z ]+)\W/';
$str = 'Project Bribara<project.bribara@gmail.com';
preg_match($re, $str, $matches);
Echo $matches[1];
// Project email
$environment['ENV_MAIL'] = "Project Bribara<project.bribara@gmail.com";
$projectEmail = $environment['ENV_MAIL'];
echo $projectEmail;
$projectName = html_entity_decode ($projectEmail);
echo $projectName;
有几种方法可以解决这个问题:(Demo Link)
所有其他答案均失败的原因是您错误地陈述了输入字符串。您的实际输入字符串包含 <
,它是 <
的 html 实体。我下面的方法将向未来的 SO 读者阐明这一点,并帮助您 select 为您的项目找到最佳方法。
$environment['ENV_MAIL']="Project Bribara<project.bribara@gmail.com";
// ^^^^ html entity causing you trouble
$decoded_string=html_entity_decode($environment['ENV_MAIL']); // convert "<" to "<"
var_export(explode('<',$decoded_string)); // generate an array of the two parts for future access
echo "\n\n";
echo "Project Name = ",strstr($decoded_string,'<',true); // use strstr on < of decoded string with "true" param
echo "\n\n";
echo "Project Email = ",substr($decoded_string,strpos($decoded_string,'<')+1); // use substr&strpos on <
echo "\n\n";
// or less potentially damaging to your input string...
var_export(explode('<',$environment['ENV_MAIL']));
输出:
array (
0 => 'Project Bribara',
1 => 'project.bribara@gmail.com',
)
Project Name = Project Bribara
Project Email = project.bribara@gmail.com
array (
0 => 'Project Bribara',
1 => 'project.bribara@gmail.com',
)