Php 匹配一年中的所有事件

Php 匹配一年中的所有事件,php,regex,Php,Regex,我试图匹配字符串中找到的所有日期,函数如下 $description = "1999 2008 1998"; if(preg_match("/[12][0-9]{3}/", $description, $matches)){ print_r($matches); } 问题是只返回第一个日期,即1999,实际上我希望匹配所有日期 我应该在正则表达式中更改什么?你是说这个吗 <?php $description = "1999 2008 1998"; if(preg_match_a

我试图匹配字符串中找到的所有日期,函数如下

$description = "1999 2008 1998";

if(preg_match("/[12][0-9]{3}/", $description, $matches)){
   print_r($matches);
}
问题是只返回第一个日期,即
1999
,实际上我希望匹配所有日期

我应该在正则表达式中更改什么?

你是说这个吗

<?php
$description = "1999 2008 1998";

if(preg_match_all("/[12][0-9]{3}/", $description, $matches)){
   print_r($matches);
}

如果年份无效怎么办7777@Baba7777不会被匹配…一直以来,我都不知道有一场预赛,真尴尬。不管怎样,谢谢你。这就是你想要的吗?最好是你问问题并学习,而不是呆在黑暗中@mk_89好吧,别失望,但我也不知道
preg_match_all
,刚开始在谷歌上输入
preg_m
,然后自动建议就吐出来了,所以我想这可能是你想要的,并写下了答案,无论如何,如果你能检查一下,这样别人就不会试图找出你已经得到的答案。
<?php
$description = "1999 2008 1998";

$a = Array(preg_match_all('/(\d{4})*/', $description, $matches));

$count = count($matches);

for ($i = 0; $i <= $count + 2; $i++) {
    echo $matches[0][$i] . "\n";
}
?>
1999
2008
1998