|
-
Jan 2nd, 2006, 12:03 PM
#1
Thread Starter
Hyperactive Member
Regular Expression to change string to array.
Hi,
I have a question about sorting the content of a string.
I have a string that contains coordinates ...
it looks like this:
Code:
"[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:
Code:
"[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:
Code:
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:
Code:
$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 !
____________________________________________
Please rate my messages. Thank you!
____________________________________________
Bram Vandenbon
http://www.bramvandenbon.com
-
Jan 2nd, 2006, 12:10 PM
#2
Re: Regular Expression to change string to array.
Code:
$numbers = explode('][', substr($str, 1, -1));
Look, no regex! Which means it's probably about 10 times as fast.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 2nd, 2006, 12:23 PM
#3
Thread Starter
Hyperactive Member
Re: Regular Expression to change string to array.
____________________________________________
Please rate my messages. Thank you!
____________________________________________
Bram Vandenbon
http://www.bramvandenbon.com
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|