Reverse order of individual items in a delimited string

Topic: Reversing the order of individual items in a string.

Specifics: I had a date contained in a GET query string that was formatted as follows:

01/27/2013

and I needed it reversed to

2013/01/27

Here is a simple method to do the reversing in PHP:

$toDate = htmlentities($_GET[‘to’]);
$toDateNew = implode(“/”,(array_reverse(explode(“/”,$toDate))));

Explanation of code: First you explode the $toDate variable, then reverse the resulting array, finally you implode that array back into a string. Of course there are other date manipulating/formatting techniques that will do this in a ┬ásingle step, but this method also works for any text/strings as long as there is a consistent delimeter. ­čÖé

Leave a Reply

Your email address will not be published. Required fields are marked *