PHP 爆炸并将缺失的部分设置为空字符串

发布于 2024-09-02 16:04:05 字数 547 浏览 8 评论 0原文

完成以下任务的最佳方法是什么。

我有这种格式的字符串:

$s1 = "name1|type1"; //(pipe is the separator)
$s2 = "name2|type2";
$s3 = "name3"; //(in some of them type can be missing)

假设 nameN / typeN 是字符串,并且它们不能包含管道。

由于我需要分别提取名称/类型,所以我这样做:

$temp = explode('|', $s1);
$name = $temp[0];
$type = ( isset($temp[1]) ? $temp[1] : '' );

是否有一种更简单(更智能、更快)的方法来执行此操作,而无需执行 isset($temp[1])< /code> 或 count($temp)

谢谢!

What's the best way to accomplish the following.

I have strings in this format:

$s1 = "name1|type1"; //(pipe is the separator)
$s2 = "name2|type2";
$s3 = "name3"; //(in some of them type can be missing)

Let's assume nameN / typeN are strings and they can not contain a pipe.

Since I need to exctract the name / type separetly, I do:

$temp = explode('|', $s1);
$name = $temp[0];
$type = ( isset($temp[1]) ? $temp[1] : '' );

Is there an easier (smarter whatever faster) way to do this without having to do isset($temp[1]) or count($temp).

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

征﹌骨岁月お 2024-09-09 16:04:05
list($name, $type) = explode('|', s1.'|');
list($name, $type) = explode('|', s1.'|');
心碎无痕… 2024-09-09 16:04:05

请注意,对于 $s3,explode() $type 的参数顺序

list($name,$type) = explode( '|',$s1);

将为 NULL,尽管它会给出通知

Note the order of arguments for explode()

list($name,$type) = explode( '|',$s1);

$type will be NULL for $s3, though it will give a Notice

夏尔 2024-09-09 16:04:05

我是 array_pop()array_shift(),如果它们使用的数组为空,则不会出错。

在你的情况下,那就是:

$temp = explode('|', $s1);
$name = array_shift($temp);
// array_shift() will return null if the array is empty,
// so if you really want an empty string, you can string
// cast this call, as I have done:
$type = (string) array_shift($temp);

I'm a fan of array_pop() and array_shift(), which don't error out if the array they use is empty.

In your case, that would be:

$temp = explode('|', $s1);
$name = array_shift($temp);
// array_shift() will return null if the array is empty,
// so if you really want an empty string, you can string
// cast this call, as I have done:
$type = (string) array_shift($temp);
乖乖哒 2024-09-09 16:04:05

不需要执行isset,因为$temp[1]将存在并且内容为空值。这对我来说效果很好:

$str = 'name|type';

// if theres nothing in 'type', then $type will be empty
list($name, $type) = explode('|', $str, 2);
echo "$name, $type";

There is not need to do isset since $temp[1] will exist and content an empty value. This works fine for me:

$str = 'name|type';

// if theres nothing in 'type', then $type will be empty
list($name, $type) = explode('|', $str, 2);
echo "$name, $type";
北方。的韩爷 2024-09-09 16:04:05
if(strstr($temp,"|"))
{
   $temp = explode($s1, '|');
   $name = $temp[0];
   $type = $temp[1];
}
else
{
   $name = $temp[0];
   //no type
}

或许?

if(strstr($temp,"|"))
{
   $temp = explode($s1, '|');
   $name = $temp[0];
   $type = $temp[1];
}
else
{
   $name = $temp[0];
   //no type
}

Maybe?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文