我试图匹配在字符串中找到的所有日期的所有出现,这里是功能匹配一年
$description = "1999 2008 1998";
if(preg_match("/[12][0-9]{3}/", $description, $matches)){
print_r($matches);
}
的问题是,只有在第一次约会时返回这是1999
,我其实想要匹配所有日期。
我应该在正则表达式中改变什么?
我试图匹配在字符串中找到的所有日期的所有出现,这里是功能匹配一年
$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);
}
唯一的区别是preg_match_all
而不是preg_match
。
<?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
如果什么年份是无效像7777 – Baba
@Baba 7777将不会匹配...... –