PHP explode and set to empty string the missing pieces
- by Marco Demaio
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!