获取php中以`and`连接的作者的姓氏
假设我有一串作者:获取php中以`and`连接的作者的姓氏,php,preg-match,Php,Preg Match,假设我有一串作者: $str1="Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France"; $str2="Evans, C. J."; 如何通过preg_match()获得他们的姓 输出应分别为: EvansEbinNirenberg Evans 谢谢 尝试使用explode() 使用preg_match() 您可以使用: /([A-Z])\w+(?=,)/g 演示: PHP代码: $str1="Evans, C. J. and E
$str1="Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France";
$str2="Evans, C. J.";
如何通过preg_match()
获得他们的姓
输出应分别为:
EvansEbinNirenberg
Evans
谢谢 尝试使用explode()
使用preg_match()
您可以使用:
/([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]);
这实际上是他们的姓。@Gumbo谢谢,我会解决这个问题的!作者由
和
联合而成,因此最后一位作者是尼伦堡,Jhon France
,最后一位作者的名字是尼伦堡
,感谢您的可选答案!在演示中似乎很有用,但是我应该用什么替换php中的/g
?我明白了,preg\u match\u all
另一个最后的问题,为什么[A-Z]
也匹配较低的后者?如果不添加/i
我明白了,也许/\b\w+\b(?=,)/
更容易理解。
$str = 'Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France';
preg_match_all('/([A-Z])\w+(?=,)/', $str, $matches);
echo implode('',$matches[0]); //EvansEbinNirenberg
$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]);