7

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!

Marco Demaio
  • 33,578
  • 33
  • 128
  • 159

5 Answers5

8
list($name, $type) = explode('|', s1.'|');
Flavius Stef
  • 13,740
  • 2
  • 26
  • 22
4

Note the order of arguments for explode()

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

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

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • will still have to check if type is null and assign ' ' to it if so though – Thomas Winsnes May 19 '10 at 16:19
  • 3
    if you do: @list($name,$type) = explode('|', $s1), the notice will be swallowed. @Thomas - leverage php's untyped nature and allow php to type-juggle the null value based on its usage. – Kevin Vaughan May 19 '10 at 16:27
  • @Mark Baker: i updated the code in the question, thanks for telling me. – Marco Demaio May 19 '10 at 16:57
  • @Marco Using Stef's trick of appending a | before the explode guarantees no notice and an empty string in $type unless there is a genuine $type value – Mark Baker May 20 '10 at 10:28
  • @Kevin Vaughan: +1 for your comment, but I did a test: it's true that with your solution `list` will not fire a Notice anymore when `$type` is missing, but `$type` would still be set to `NULL` and NOT to empty string, you can test with a simple `var_dump($type);` – Marco Demaio May 06 '11 at 19:06
3

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);
pinkgothic
  • 6,081
  • 3
  • 47
  • 72
0

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";
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • I don't undertand the 2 limit you used in explode, what for? – Marco Demaio May 19 '10 at 17:08
  • you are right, but actually during development I use `error_reporting(E_ALL);` and when `$str='name'`, the call to `list($name, $type)` rises a PHP error `Notice: Undefined offset: 1` – Marco Demaio May 06 '11 at 19:00
-1
if(strstr($temp,"|"))
{
   $temp = explode($s1, '|');
   $name = $temp[0];
   $type = $temp[1];
}
else
{
   $name = $temp[0];
   //no type
}

Maybe?

DrLazer
  • 2,805
  • 3
  • 41
  • 52
  • 1
    Just a heads-up: If all you want to do is check if a needle-string is in a haystack-string, then you should use `strpos() !== false`, it's significantly faster than `strstr()`. – pinkgothic May 19 '10 at 17:21