PHP explode and set to empty string the missing pieces
Posted
by Marco Demaio
on Stack Overflow
See other posts from Stack Overflow
or by Marco Demaio
Published on 2010-05-19T16:07:51Z
Indexed on
2010/05/19
16:10 UTC
Read the original article
Hit count: 225
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!
© Stack Overflow or respective owner