查找月孕期匹配

2022-03-29 00:00:00 string php preg-match

我正在通过cURL检查存储在PHP变量中的网页。在网页内将是一个月的名称和年份,也是一个名称,因此网页将如下所示:

December 2012
Name: Jack Brown

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

For Jack Brown - December 2012

我已尝试

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

但出现错误


解决方案

我为您制作了一个代码段,以便您可以从此处开始

$str = 'December 2012 Name: Jack Brown';
$ptr = "/^(?P<month>:Jan(?:uary)?|Feb(?:ruary)?|Dec(?:ember)?) (?P<year>:19[7-9]d|2d{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'

PS。你可以加更多月我才懒加,干杯

相关文章