完成以下操作的最佳方式是什么?PHP爆炸并设置为空字符串丢失件
我有一个字符串格式为:
$s1 = "name1|type1"; //(pipe is the separator)
$s2 = "name2|type2";
$s3 = "name3"; //(in some of them type can be missing)
假设nameN
/typeN
是字符串,它们不能包含管道。
因为我需要exctract名称/类型separetly,我做的:
$temp = explode('|', $s1);
$name = $temp[0];
$type = (isset($temp[1]) ? $temp[1] : '');
是否有更简单(聪明任何更快)的方式来做到这一点,而不必做isset($temp[1])
或count($temp)
。
谢谢!
不要这样想,如果你试图访问你会得到一个异常/错误'[1]'没有检查。 – Aren 2010-05-19 16:10:34
这看起来像是最有效的解决方案。 – EAMann 2010-05-19 16:14:01