Reply to Re: Regex help

Your name:

Reply:


Posted by Toby Inkster on 10/10/05 21:57

BernieH wrote:

> I need a regex which will extract the year only from a variety of date
> strings such as -
>
> April 8, 2000
> 13 May 1999
> April 1999
> 1999
> 20050908 (yyyymmdd format)

It's unlikely a regexp would be able to do that. If you're using PHP,
check out the strtotime() function.

<?php
$testdates = array (
"April 8, 2000",
"13 May 1999",
"April 1999",
"1999",
"20050908",
"Next Tuesday",
"Yesterday",
"6 years"
);

while ( $somedate = array_pop($testdates) )
{
$year = (int)date("Y", $somedate);
print "<p>DATE is $date<br>YEAR is $year</p>\n";
}
?>

If you really, really need to use a regexp, the following will match a
year in many cases:

/[12][0-9]{3}/

but it won't be 100% reliable.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация