View Full Version : How to separate this string?
BrandonR
04-09-2004, 04:34 PM
So I'm working on a PHP program that will store a record of places my users have been to... Each of the places have a unique ID so that in the mySQL database it will display the list of places in a string like this:
<12><455><153><5>
With each number within the arrows corresponding to an item... My question is, whats the best way to separate a string like that into the separate numbers with it being split between the "><" of each number?
I tried tokenizing the string but that didn't work too well... Any ideas?
gold_dragon
04-11-2004, 04:11 PM
Originally posted by BrandonR
So I'm working on a PHP program that will store a record of places my users have been to... Each of the places have a unique ID so that in the mySQL database it will display the list of places in a string like this:
<12><455><153><5>
With each number within the arrows corresponding to an item... My question is, whats the best way to separate a string like that into the separate numbers with it being split between the "><" of each number?
I tried tokenizing the string but that didn't work too well... Any ideas? Don't split unless you the string is 12><455><153><5What you want to do is have a regular expression run on it:/[<]([0-9]+)[>]/gLet me explain what this should do (Regular Expressions sometimes have a mind of their own, which actually to the fault of the programmer). You are looking for a '<' character followed by a number that is more than one but can be as big as you want it to be. Make this a class and you are able to backreference it to use later. After the class you are looking for the closing bracket, '>'. Search this for as many as there is and you are done.
You could also split on '<' first and then split on '>' and find which elements have a number in them.
skidooer
04-14-2004, 03:23 AM
Originally posted by gold_dragon
Don't split unless you the string is What you want to do is have a regular expression run on it:/[<]([0-9]+)[>]/g
That regexp isn't ideal for split (at least PCRE). Every other element will be blank.
You need something more like:
/\D+/
Which will still leave the first array element blank, but that's easy enough to shift off.
gold_dragon
04-14-2004, 10:03 AM
Originally posted by skidooer
That regexp isn't ideal for split (at least PCRE). Every other element will be blank.
You need something more like:
/\D+/
Which will still leave the first array element blank, but that's easy enough to shift off. I thought \D would look for something that isn't a number... which is what you would want to split on but it would leave a really big number with no way to sort out the numbers. I'm sorry, this is not true of split, it is true for replace.
The regular expression wasn't for spliting it was for getting the back-reference. Which in php you would:preg_match_all( "/[<]([0-9]+)[>]/", "<12><455><153><5>", $matches);This should allow you to get the numbers by (I can't test this but I will debug it when I get home):echo $matches[1][0], "
", $matches[1][1], "
";
echo $matches[1][2], "
", $matches[1][3];This should output:12
455
153
5But could always split like above. Mine with two steps, the other with one. I would use the one if you want to split.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.