正则表达式使用PHP在字符串中查找名称

时间:2016-10-10 22:29:55

标签: php

我想使用正则表达式查找字符串中的所有名称。如果我抓住不是名字的单词或单词组,我不介意。

我是正则表达式的新手,如果有人可以提供帮助,我将不胜感激。

$str = 'It was first suggested by Tom Boyd then afterwords by William H John Smith on Monday. But on Wednesday Simon Nixon did not agree stating that he and Terry Wallace, Joan C and K. Harris categorically disagreed with Tom Boyd and William H John Smith.';
preg_match_all("/[A-Z][a-z']+(?: [A-Z][a-z]+)*/",$str,$matches);
print_r($matches);

这给了

Array
(
    [0] => Array
        (
            [0] => It
            [1] => Tom Boyd
            [2] => William
            [3] => John Smith
            [4] => Monday
            [5] => But
            [6] => Wednesday Simon Nixon
            [7] => Terry Wallace
            [8] => Joan
            [9] => Harris
            [10] => Tom Boyd
            [11] => William
            [12] => John Smith
        )

)

错误是William H John Smith,周三Simon Nixon,Joan C和K. Harris。

0 个答案:

没有答案