完成以下任务的最佳方法是什么。
我有这种格式的字符串:
$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])
或count($temp)
.
谢谢!