-2

我正在通过 CURL 检查存储在 PHP 变量中的网页。网页内将包含月份名称和年份以及名称,因此网页看起来像:

December 2012
Name: Jack Brown

我想要一个预匹配来查找数据,所以我可以像这样显示它:

For Jack Brown - December 2012

我试过了

\b(?:Jan(?:uary)?|Feb(?:ruary)?|...|Dec(?:ember)?) (?:19[7-9]\d|2\d{3})(?=\D|$) 

但它出现了一个错误

4

1 回答 1

1

我为你做了一个片段,所以你可以从这里开始

$str = 'December 2012 Name: Jack Brown';
$ptr = "/^(?P<month>:Jan(?:uary)?|Feb(?:ruary)?|Dec(?:ember)?) (?P<year>:19[7-9]\d|2\d{3}) (Name:(?P<name>(.*)))/";
preg_match($ptr, $str, $data);
echo 'For '.trim($data['name']).' - '.$data['month'].' '.$data['year'];

结果将是

'For Jack Brown - December 2012'

这是一个数组

Array
(
    [0] => December 2012 Name: Jack Brown
    [month] => December
    [1] => December
    [year] => 2012
    [2] => 2012
    [3] => Name: Jack Brown
    [name] =>  Jack Brown
    [4] =>  Jack Brown
    [5] =>  Jack Brown
)

我也尝试

'February 2005 Name: Bob' => 'For Bob - February 2005'

附言。你可以添加更多月份,我只是懒得添加,干杯

于 2012-12-13T05:05:18.117 回答