19

I've written what I thought was a very simple use of the php explode() function to split a name into forename and surname:

// split name into first and last
$split = explode(' ', $fullname, 2);
$first = $split[0];
$last = $split[1];

However, this is throwing up a php error with the message "Undefined offset: 1". The function still seems to work, but I'd like to clear up whatever is causing the error. I've checked the php manual but their examples use the same syntax as above. I think I understand what an undefined offset is, but I can't see why my code is generating the error!

SilentGhost
  • 264,945
  • 58
  • 291
  • 279
musoNic80
  • 3,548
  • 9
  • 37
  • 48

7 Answers7

47

this is because your fullname doesn't contain a space. You can use a simple trick to make sure the space is always where

 $split = explode(' ', "$fullname ");

(note the space inside the quotes)

BTW, you can use list() function to simplify your code

  list($first, $last) = explode(' ', "$fullname ");
user187291
  • 50,823
  • 18
  • 89
  • 127
  • That's great. I didn't realise I could put a variable inside quotes. Also, thanks for the bonus 'list()' tip. – musoNic80 Nov 27 '09 at 18:49
  • That's not true, if explode doesn't find the string you are using it'll return an array with a single position and the full text in that position. – Khriz Jun 20 '12 at 09:31
  • 4
    you need as many split chars as var to assign: e.g. `list($size, $ctype, $sample, $desc) = explode(',', $item.',,,,');` so it's assured you always get an assignment – sherpya Feb 24 '14 at 02:48
  • Be careful with this hack, as with third parameter in `explode` it can be tricky ie: `$string = 'abc abc'; list($first, $last) = explode(' ', "$string ", 2);`. The result of $last is: "abc " with space at the end! – instead Jun 21 '15 at 20:41
  • This is a great trick. To simplify your code even further, you don't need list() at all: [$first, $last] = explode(' ', "$fullname "); – phoenix Jul 09 '20 at 20:15
  • Works for PayPal IPN listener code `explode("=", "$lines[$i]=");` - adding the `=` prevents the error where there is no `=` in the returned post string. – Steve Sep 14 '20 at 11:48
13

Use array_pad

e.q.: $split = array_pad(explode(' ', $fullname), 2, null);

  • explode will split your string into an array without any limits.
  • array_pad will fill the exploded array with null values if it has less than 2 entries.

See array_pad

phse
  • 2,148
  • 2
  • 18
  • 13
4

This could be due the fact that $fullname did not contain a space character.

This example should fix your problem w/o displaying this notice:

$split = explode(' ', $fullname, 2);
$first = @$split[0];
$last = @$split[1];

Now if $fullname is "musoNic80" you won't get a notice message.

Note the use of "@" characters.

HTH Elias

Community
  • 1
  • 1
Elias Bachaalany
  • 1,152
  • 2
  • 13
  • 26
  • 3
    Makes sense, but I'd rather find a way of fixing the problem rather than supressing the errors. Thanks though for explaining the cause! – musoNic80 Nov 27 '09 at 18:47
4

BTW, that algorithm won't work all the time. Think about two-word Latina or Italian surnames names like "De Castro", "Dela Cruz", "La Rosa", etc. Split will return 3 instead of 2 words:

Array {
  [0] => 'Pedro'
  [1] => 'De'
  [1] => 'Castro'
}

You'll end up with messages like "Welcome back Ana De" or "Editing Profile of Monsour La".

Same thing will happen for two-word names like "Anne Marie Miller", "William Howard Taft", etc.

Just a tip.

Jhourlad Estrella
  • 3,275
  • 3
  • 31
  • 56
  • Changing the third flag in `explode(' ', $fullname, 2)` to `explode(' ', $fullname, 1)` solves this. – SQueryL Oct 14 '18 at 19:34
2

Presumably, whatever $fullname is doesn't contain a space, so $split is an array containing a single element, so $split[1] refers to an undefined offset.

Dominic Rodger
  • 90,548
  • 30
  • 192
  • 207
1

array_pad is the only valid answer in this thread, all others are ugly hacks that can explode into your face. With array_pad you can always be sure that the amount of elements is correct and filled. Especially important when using with a list() type output.

Kirill Matrosov
  • 3,620
  • 2
  • 22
  • 34
0

That' strange, it's working correct here. When i try with a string the cat walks and also just the will do and not produce an error. I've outputted it with print_r

What's your $fullname looks like when you get the error?

Ben Fransen
  • 10,136
  • 16
  • 67
  • 123