Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird date parsing... 2

Status
Not open for further replies.

admoore

IS-IT--Management
Joined
May 17, 2002
Messages
224
Location
US
I need to convert the returned year string into a very weird format, that is:

2003 becomes 23
2004 becomes 24

and so on until

2010 becomes 210, etc...

No need to worry about years past or beyond, lets say 2099...

TIA, as always...

-Allen M
 
Use a regular expression with preg_replace.

$string = "2010";
$pattern = "/(2)([0]+)([^0])/";
$replacement = "\$1\$3";
print preg_replace($pattern, $replacement, $string);

Explanation:
(2) starts with a 2
[0]+ one or more zeros
[^0] any carachter not zero
The parts are tagged with ().
The first subpattern is $1, the second $2 etc.
 
Just as an addendum, there's no need to capture what you won't use... to get the same effect but save a tiny amount of memory you could've done...

Code:
$string = "2010";
$pattern = "/(2)[0]+([^0])/";
$replacement = "\$1\$2";
print preg_replace($pattern, $replacement, $string);

and lately it seems like
Code:
$replace = "$1$2";

works as well, but I'm not sure if that is a feature or a bug. Anyway, take it as you will, I just figured if you're new to Regex's you should know about the ability to just capture what you want.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top