开发者

PHP - preg_match - check if string contains year which is between 1950 and 2010

How do I check with p开发者_高级运维reg_match if string contains year which is between 1950 and 2010 ?

example:

$string = "I was born in 1986 year.";


Assuming you have a timestamp:

$date = getdate($timestamp);
if ($date['year'] >= 1950 && $date['year'] <= 2010)
  return 'good';


If the string is already formatted in a way that can be consumed by getdate(), the other answers would be the best solution.

If the string is just some random text which might or might not contain a date, you'd need to use a regex to find those numbers.

/(19[5-9][0-9]|20(0[0-9]|10))/

Of course, you have no guarantee that the numbers matched this way is actually a year. It could be 2005 pounds of steel or 1976 miles of highway.


$date = getdate( $timestamp );
if( (int)$date['year'] >= 1950 && (int)$date['year'] <= date('Y') ) {
   return 'good';
}

:P

if( preg_match( '/^(19[5-9]{1}[0-9]{1}|20(0[0-9]{1}|10))$/', $i ) ) {}
// tested - works. 1950-2010
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜