Splitting string into names and numbers?


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

$name[0]=“Fred” $number[0]=“22”
$name[1]=“Bob” $number[1]=“55”
$name[2]=“Jane” $number[2]=“79”

thanks in advance

You can split the string using explode.

I was thinking about maybe putting the into arrays but am no sure if thats the best way or not

The best way depends on what you need to do with the data.

Thanks, I did play about with explode but it didnt create the arrays as expected.

I will be using the “name” string to match to a “nameID” from a mysql table, and inserting the “number” string and “nameID” into another table

I hope that makes sense

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);

The result of this code should be:

    [0] => Array
            [0] => Fred 22
            [1] => Bob2 55
            [2] => Jane 79

    [1] => Array
            [0] => Fred
            [1] => Bob2
            [2] => Jane

    [2] => Array
            [0] => 22
            [1] => 55
            [2] => 79


As you can see, the names are in the $arr[1] array, and the values in the $arr[2] array.

Great. thanks for your help, I will give that a try later

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.

This could go so wrong if your incoming string is not strictly in the format <string><space><integer> though.

Example - $string=“data: Fred 22 Bob 55 Jane 79”;

If you want a more rigorous solution you will have to explain more about the string and where it is coming from.

Np :slight_smile:

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.

The non-regex way:

$arr = explode(" ", $string);
$nums = array_filter($arr,"is_int");
$names = array_diff($arr,$nums);

The Regex is cleaner in this instance, of course… but doesnt quite match correctly.

would match “John A1234” as well… Why is the extra [^\s] there, Guido?

@mattgoffrey I was meant to be just posing the question - though it did not come out like that - I was not deriding your solution.

I often find that by probing the OP we get to the real nub of the problem.

Hmm, I guess it’s a left over from my tests.
This would work:
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:
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:

arr : Array ( [0] => Fred [1] => 22 [2] => Bob2 [3] => 55 [4] => Jane [5] => 79 [6] => John [7] => A1234 )
names : Array ( [0] => Fred [2] => Bob2 [4] => Jane [B][6] => John [7] => A1234[/B] )
nums : Array ( [1] => 22 [3] => 55 [5] => 79 ) 

And the keys would not be the same in both arrays.

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…

is_int doesn’t work, I tested it. It gives FALSE on all values in the array.
In the manual I read this:


To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

So I tried is_numeric() and it worked (even though it would catch a lot of other things as well).

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 :slight_smile: