Say I have a string
"example1string"
I would like to turn it into
"1examplestring"
I am experimenting with methods to be able to reorder an array of strings by number using the natsort() method, however it doesn’t seem to work correctly when the number is in the middle of the string.
I have searched for a solution but have so far found nothing with this specific issue.
2
Answers
I’m sure there is a more efficient
regex
but here’s a greedy way:Try the below PHP function, Please note that as Rob asked in the comment, what happens when a string has multiple digits
like wwehehejr21jfkr44j with the below example the outcome will be 2144wwehehejrjfkrj
Your question also had said reorder, did you mean if a string is wwehehejr21jfkr44j the output should be 1244wwehehejrjfkrj ?
Okay now if you meant that if a if a string like wwehehejr21jfkr44j is given the output should be 1244wwehehejrjfkrj then use the below code by adding the sort like below:
Good luck, Amigo!