PDA

Click to See Complete Forum and Search --> : Regular Expression to change string to array.


BramVandenbon
Jan 2nd, 2006, 11:03 AM
Hi,
I have a question about sorting the content of a string.

I have a string that contains coordinates ...
it looks like this:
"[aa][bc][sa][dd][qq][se][zd][dk][ks]"

each coordinate looks like this:
[..]

This string needs to be sorted so that I get the following result:
"[aa][bc][dd][dk][ks][sa][se][qq][zd]"

What is the best way to do this?

I was thinking to do it the following way:
First I would parse all coordinates by using a for-loop and the substr function. And I would put them in an array. Next I would sort the array with the sort() function.

So after the sorting the array would look like this:

aa
bc
dd
dk
ks
sa
se
qq
zd

I was just thinking... Is there a faster way?

I saw somebody solving a simular problem by using regular expressions:
$list = preg_split('/;\s*/', $str);

The problem is: I forgot how to create regular expressions. I was just wondering, is there somebody who can spare me 2 hours of reading, studying and testing and give me the pattern :). That would be very nice!

Thank you very much in advance !

CornedBee
Jan 2nd, 2006, 11:10 AM
$numbers = explode('][', substr($str, 1, -1));
Look, no regex! Which means it's probably about 10 times as fast.

BramVandenbon
Jan 2nd, 2006, 11:23 AM
Nice Job ! :-) Thank you