Page 1 of 1

Why am i getting this output?

Posted: Thu Sep 11, 2008 1:23 am
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.

Re: Why am i getting this output?

Posted: Thu Sep 11, 2008 2:40 am
by Ziq
You use not correct pattern.
Try this:

Code: Select all

 
$pattern = '|([\d]{2})/([\d]{2})/([\d]{2})|';
 

Re: Why am i getting this output?

Posted: Thu Sep 11, 2008 5:46 pm
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]