Why am i getting this output?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Why am i getting this output?

Post by swetha »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Code: Select all

<?php
$string = '12/25/08';
$pattern = '|(d{2})/(d{2})/(d{2})|';
$replacement = '$3$1$2';
echo preg_replace($pattern, $replacement, $string);
?>
ive tried running the above code which was taken from another site.
the output of the site should have been 081225,but the output that i get is 12/25/08.
Why am i not getting the desired output?


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Why am i getting this output?

Post by Ziq »

You use not correct pattern.
Try this:

Code: Select all

 
$pattern = '|([\d]{2})/([\d]{2})/([\d]{2})|';
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Why am i getting this output?

Post by pickle »

Best not to use regex if possible, as the regex engine creates a lot of overhead.

Code: Select all

$string = '12/25/08';
$exploded = explode('/',$string);
echo $exploded[3],$exploded[2],$exploded[1]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply