【发布时间】:2016-10-22 12:06:38
【问题描述】:
可以说我有以下价值,我想拥有岁月
July 1967 USA
14 March 1979 France
23 June 1976 France
from the 21 October 2016
假设这个值是 $year2 我正在使用 is_numeric 但没有像往常一样给出正确的结果可以使用 preg_replace
$year2 = explode(" ",$year1);
if (!is_numeric($year2[2])) {
if ( ! isset($year2[1])) {
$year2[1] = null;
}
$year = $year2[1];
} else {
if ( ! isset($year2[2])) {
$year2[2] = null;
}
$year = $year2[2];
}
}
echo $year;
输出有时是日期 ex: 21 而不是 2016 年
我试过这个没有成功$year = preg_match('/\d{4}/g',$year2);
【问题讨论】:
-
我建议使用正则表达式
-
请问该怎么做?
-
好,你已经接近解决方案了,你只需要看一下php手册中的
preg_match,看看它是如何工作的(匹配结果不会像你想象的那样返回)。其他的g修饰符在 PHP 中不存在,要执行全局研究,您需要使用preg_match_all: php.net/manual/en/function.preg-match-all.php
标签: php preg-match isnumeric