在 php 中获取由 `and` 连接的作者的姓氏
get the last name of authors joined by `and` in php
假设我有一串作者:
$str1="Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France";
$str2="Evans, C. J.";
如何通过 preg_match()
获取他们的 名 姓氏?
输出应该分别是:
EvansEbinNirenberg
Evans
谢谢!
试试 explode()
$str1="Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France";
$last_names = '';
$s = explode(',', $str1);
foreach($s as $v) {
$n[] = explode(' ',$v);
}
foreach($n as $ln) {
$last_names .= end($ln);
}
echo $last_names; //EvansEbinNirenbergFrance
与 preg_match()
$str = 'Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France';
preg_match_all('/([A-Z])\w+(?=,)/', $str, $matches);
echo implode('',$matches[0]); //EvansEbinNirenberg
您可以使用:
/([A-Z])\w+(?=,)/g
PHP代码:
$str1="Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France";
$str2="Evans, C. J.";
preg_match_all('/([A-Z])\w+(?=,)/',$str1,$matches);
echo implode('',$matches[0])."\n";
preg_match_all('/([A-Z])\w+(?=,)/',$str2,$matches);
echo implode('',$matches[0]);
假设我有一串作者:
$str1="Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France";
$str2="Evans, C. J.";
如何通过 preg_match()
获取他们的 名 姓氏?
输出应该分别是:
EvansEbinNirenberg
Evans
谢谢!
试试 explode()
$str1="Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France";
$last_names = '';
$s = explode(',', $str1);
foreach($s as $v) {
$n[] = explode(' ',$v);
}
foreach($n as $ln) {
$last_names .= end($ln);
}
echo $last_names; //EvansEbinNirenbergFrance
与 preg_match()
$str = 'Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France';
preg_match_all('/([A-Z])\w+(?=,)/', $str, $matches);
echo implode('',$matches[0]); //EvansEbinNirenberg
您可以使用:
/([A-Z])\w+(?=,)/g
PHP代码:
$str1="Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France";
$str2="Evans, C. J.";
preg_match_all('/([A-Z])\w+(?=,)/',$str1,$matches);
echo implode('',$matches[0])."\n";
preg_match_all('/([A-Z])\w+(?=,)/',$str2,$matches);
echo implode('',$matches[0]);