undefined offset when using php explode() [duplicate]
Asked Answered
R

7

21

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!

Resignation answered 27/11, 2009 at 10:11 Comment(1)
Can you provide the value of $fullname that gives you the error?Macula
E
50

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 ");
Egidius answered 27/11, 2009 at 10:18 Comment(5)
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.Spake
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 assignmentLianne
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!Enos
This is a great trick. To simplify your code even further, you don't need list() at all: [$first, $last] = explode(' ', "$fullname ");Thallophyte
Works for PayPal IPN listener code explode("=", "$lines[$i]="); - adding the = prevents the error where there is no = in the returned post string.Miche
R
23

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

Rescissory answered 10/7, 2019 at 13:4 Comment(0)
C
5

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

Consultation answered 27/11, 2009 at 10:16 Comment(1)
Makes sense, but I'd rather find a way of fixing the problem rather than supressing the errors. Thanks though for explaining the cause!Resignation
B
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.

Bergama answered 14/4, 2011 at 5:46 Comment(1)
Changing the third flag in explode(' ', $fullname, 2) to explode(' ', $fullname, 1) solves this.Whitefly
A
3

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.

Asarum answered 27/11, 2009 at 10:16 Comment(0)
T
3

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.

Theorbo answered 10/9, 2019 at 8:12 Comment(0)
T
1

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?

These answered 27/11, 2009 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.