【发布时间】:2018-04-06 19:39:16
【问题描述】:
内容结构如下:
$contents = '1234 FIRSTNAME LASTNAME M 4321
1345 LASTNAME F 4621
8223 FIRSTNAME LASTNAME M 4256;
我只想提取数组中的名字或姓氏,如下所示:
Array ( [0] => FIRSTNAME LASTNAME,
[1] => LASTNAME )
我的代码:
<?php
$contents = '1234 FIRSTNAME LASTNAME M 4321
1345 LASTNAME F 4621
8223 FIRSTNAME LASTNAME M 4256';
$res = preg_replace('/([A-Z]{2,24})\s+([A-Z]{2,24})/', '$1 $2', $contents);
preg_match_all('/([A-Z]{2,24}?\s[A-Z]{2,24})/', $res, $result);
print_r($result[1]);
【问题讨论】:
-
这看起来像一个制表符分隔的文件,这是由计算机生成的吗?像3v4l.org/CrjP9 这样的东西可以工作,但如果它被分隔我会推荐php.net/manual/en/function.fgetcsv.php
-
另外
1234 FIRSTNAME LASTNAME M 4321 1345 LASTNAME F 4621' 8223 FIRSTNAME LASTNAME M 4256;无效,这是示例还是您的实际代码? -
除了说cmets,你不需要
preg_replace。去一个preg_match_all:preg_match_all('/[A-Z]{2,24}(?:\s+[A-Z]{2,24})?/', $contents, $result); -
我不小心添加了引用,现在删除了
-
感谢@revo,完美运行。我只是在寻找可选匹配。你做得很好
标签: php regex preg-replace preg-match preg-match-all