php 使用phpexplode()时未定义的偏移量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1807849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
undefined offset when using php explode()
提问by musoNic80
I've written what I thought was a very simple use of the php explode()function to split a name into forename and surname:
我写了一个我认为非常简单的使用 phpexplode()函数将名称拆分为名字和姓氏的内容:
// 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 manualbut 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!
但是,这会引发带有消息的 php 错误"Undefined offset: 1"。该功能似乎仍然有效,但我想清除导致错误的任何内容。我已经检查了php 手册,但他们的示例使用与上面相同的语法。我想我明白什么是未定义的偏移量,但我不明白为什么我的代码会产生错误!
回答by user187291
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() 函数来简化您的代码
list($first, $last) = explode(' ', "$fullname ");
回答by Elias Bachaalany
This could be due the fact that $fullnamedid not contain a space character.
这可能是由于$fullname不包含空格字符的事实。
This example should fix your problem w/o displaying this notice:
此示例应该可以解决您的问题,而无需显示此通知:
$split = explode(' ', $fullname, 2);
$first = @$split[0];
$last = @$split[1];
Now if $fullnameis "musoNic80"you won't get a notice message.
现在,如果$fullname是,"musoNic80"您将不会收到通知消息。
Note the use of "@"characters.
注意"@"字符的使用。
HTH Elias
HTH埃利亚斯
回答by Jhourlad Estrella
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:
顺便说一句,该算法不会一直有效。考虑两个单词的拉丁或意大利姓氏,如“De Castro”、“Dela Cruz”、“La Rosa”等。 Split 将返回 3 个而不是 2 个单词:
Array {
[0] => 'Pedro'
[1] => 'De'
[1] => 'Castro'
}
You'll end up with messages like "Welcome back Ana De" or "Editing Profile of Monsour La".
您最终会收到诸如“欢迎回到 Ana De”或“编辑 Monsour La 的个人资料”之类的消息。
Same thing will happen for two-word names like "Anne Marie Miller", "William Howard Taft", etc.
同样的事情也会发生在“安妮·玛丽·米勒”、“威廉·霍华德·塔夫脱”等两个词的名字上。
Just a tip.
只是一个提示。
回答by phse
Use array_pad
用 array_pad
e.q.:
$split = array_pad(explode(' ', $fullname), 2, null);
等式:
$split = array_pad(explode(' ', $fullname), 2, null);
explodewill split your string into an array without any limits.array_padwill fill the exploded array withnullvalues if it has less than2entries.
explode将您的字符串拆分为一个没有任何限制的数组。array_padnull如果分解的数组少于2条目,则将用值填充分解的数组。
See array_pad
回答by Dominic Rodger
Presumably, whatever $fullnameis doesn't contain a space, so $splitis an array containing a single element, so $split[1]refers to an undefined offset.
据推测,无论$fullname是什么都不包含空格,因此$split包含单个元素的数组也是如此,因此$split[1]指的是未定义的偏移量。
回答by Clemens Schwaighofer
array_padis the only valid answer in this thread, all others are ugly hacks that can explode into your face. With array_padyou can always be sure that the amount of elements is correct and filled.
Especially important when using with a list()type output.
array_pad是该线程中唯一有效的答案,所有其他答案都是丑陋的黑客,可以爆炸到您的脸上。使用array_pad您可以始终确保元素的数量是正确的和填充的。与list()类型输出一起使用时尤其重要。
回答by Ben Fransen
That' strange, it's working correct here. When i try with a string the cat walksand also just thewill do and not produce an error. I've outputted it with print_r
这很奇怪,它在这里工作正常。当我尝试使用字符串时the cat walks,也只会the执行并且不会产生错误。我已经输出了print_r
What's your $fullnamelooks like when you get the error?
$fullname当你得到错误时,你的样子是什么?

