0

I am trying to sort a 4 character string thats being feed in from a user into a different order. an example might be they type "abcd" which I then take and turn it into "bcad".

Here is an example of my attempt which is not working :P

<?php

$mixedDate = $_REQUEST['userDate'];

$formatted_date = firstSubString($mixedDate,2).secondSubString($mixedDate,3).thirdSubString($mixedDate,1).fourthSubString($mixedDate,4);

//... maybe some other stuff here then echo formatted_date
?>

any help would be appreciated.

6
  • What are your first, second, etc... SubString methods? You could pretty simply do this by doing something like: $formatted_date = $mixedDate[1].$mixedDate[2].$mixedDate[0].$mixedDate[3];. Commented Jul 8, 2011 at 2:04
  • 1
    Im confused. Is this suppose to be random? But then your code says userDate. Are you trying to rearrange the date? You know there are already functions for this built-in to php if thats the case. Also remember a string is just a character array, so you can manipulate it that way too. Commented Jul 8, 2011 at 2:05
  • @Matt, sorry its not the actual date its a code off of a electrical part that makes no sense but it is date of manufacture in a weird format. Commented Jul 8, 2011 at 2:09
  • @C.Johns so is it a date then? Whats the format of the date? Commented Jul 8, 2011 at 2:13
  • sorry for the late reply.. I have had to do a bunch of testing as there is alot of value manipulation going on between hex and ints etc etc. but just to inform you @Nightfirecat has it right. thankyou. Commented Jul 8, 2011 at 2:24

5 Answers 5

1

Copied from comment:

You could pretty simply do this by doing something like:

$formatted_date = $mixedDate[1].$mixedDate[2].$mixedDate[0].$mixedDate[3];

That way, you don't have to bother with calling a substring method many times, since you're just moving individual characters around.

Sign up to request clarification or add additional context in comments.

Comments

1
<?php
$mixedDate = $_REQUEST['userDate'];

$formatted_date = $mixedDate{1}.$mixedDate{2}.$mixedDate{0}.$mixedDate{3};

echo $formatted_date;
?>

The curly syntax allows you to get just that one character from your string.

It should be noted that this works correctly on your sample string, abcd and turns it into bcad if $_REQUEST['userDate'] is abcd.

3 Comments

Good point, I'll have to remove that cast to int thing. I made the stupid assumption that the date would be a number, like 2011, but then nothing is where that validates that assumption. That last is was meant to be an if, fixed thanks.
+1 for understanding the OP. Also, check your final line, you noted userData but userDate in your code.
Thank you, I fixed that also. That's what happens when I type by hand and not copy and paste! BTW, I did +1 your answer, so I hope your time was worth it at least.
0

Look into split() in php. It takes a string and a delimiter then splits the string into an array. Either force the user to use a certain format or use a regex on the input string to put the date into a known format, like dd/mm/yyyy or dd-mm-yyyy, then use the hyphen or / as the delimiter.

Once the string is split into an array, you can rearrange it any way you like.

1 Comment

given the input specified, I'd say str_split() would be more suitable than split(). (his variable names seem to imply its a date, but the code and the question imply that it doesn't have any delimiters, and that he's dealing with single characters)
0

That is very simple.

If

$mixedDate = 21-12-2010

then, try this

echo substr($mixedDate, 3, 2).'-'.substr($mixedDate, 0, 2).'-'.substr($mixedDate, 6);

this will result in

12-21-2010

This is assuming the format is fixed.

Comments

0

Use str_split() to break the string into single characters:

$char_array = str_split($input_string);

If you know exactly what order you want, and you only have four characters, then from here you can actually just do it the way you wanted from your question, and concatenate the array elements back into a single string, like so:

$output_string = $char_array[2].$char_array[3].$char_array[1].$char_array[4];


If your needs are more complex, you can sort and implode the string:

Use sort() to put the characters into order:

sort($char_array);

Or one of the other related sorting functions that PHP provides if you need a different sort order. If you need an sort order which is specific to your requirements, you can use usort(), which allows you to write a function which defines how the sorting works.

Then re-join the characters into a single string using implode():

$output_string = implode($char_array);

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.