I am looking for a bit of advice on the best way to split up a string containing names and numbers, im a bit of a newbie so any advice would be welcome.
Example - $string=“Fred 22 Bob 55 Jane 79”
I want to extract the names and numbers from the string into seperate variables, also each number relates/links to the names.
Example - 22 relates to Fred, 55 to Bob and 79 to Jane
I was thinking about maybe putting the into arrays but am no sure if thats the best way or not
Yes, explode will give you all values (names and numbers) in a single array. You’d then have to loop through it and put all uneven elements in the names array, and all even elements in the numbers array.
Or you could use a regex, something like
preg_match_all('$([^\\s]+)\\s([^\\s][\\d]+)$', 'Fred 22 Bob2 55 Jane 79', $arr);
print_r($arr);
What I would do with this first is split the string by space. This will give you an array.
The array would be even numbers of the array will have the names and the odds the numbers. It wouldn’t take much programming to insert that into your db from there.
Now, that does assume that what you need to do is as simple as the above.
I agree that it could go wrong if the data isn’t formatted as he said.
However, he gave a simple problem to which I gave a simple answer. I don’t believe in making something complicated just to cover contingencies that shouldn’t occur.
You could parse the string character by character and figure out where numeric characters start and end and then substring out the information based on that. In that way you could have first <space> last <space> number … and still peel out the number corresponding to that first and last name.
Hmm, I guess it’s a left over from my tests.
This would work:
$([^\s]+)\s([\d]+)$
It wouldn’t match “John A1234”, so John would be ignored.
And if the data is always in the correct format, then this would work too:
$([^\s]+)\s([^\s]+)$
But it would match “John A1234” as well, so you’d have to check the values of the numbers array. Something you’d have to do with all other solutions as well I think?
By the way, I liked your solution and tested it, but I think instead of “is_int” it should be “is_numeric”?
And it has some difficulties with “John A1234” as well:
is_numeric would catch a lot of other things (floats, hex, etc) - the indication was that the numbers would be ints.
You would have to do some validation afterwards (you will have to with any method tried. All methods assume correct input.); didnt think about the array keys, though you could just array_values each array…
And I apologize if it sounded like I was biting back at you or something, that wasn’t my intention either. Honestly, it didn’t sound like you were deriding the solution simply giving a suggestion to the OP. That’s why I gave a secondary solution based on what you had said.
Again, my apologies for making it seem as if I was biting back at you.
And for the record the OP did make it sound like this was going to be a simple string which would mean there’s a pretty low chance of things like hex showing up.
Of course that depends GREATLY on where that string is coming from, how it’s entered, and by whom.
Fwiw, I think it is well worth highlighting the fact that without checking the integrity of the input, things could go wrong. If either of the values has been missed for any reason, using any method is going to result in a mess. If the OP has no need for error checking, the suggestion can be ignored, but if it is something they haven’t considered, highlighting the possibility is beneficial