-2

I am a beginner with PHP and I would like to know how can I get the parts x, y, z, and t of a sequence of characters which has the form "x, y, z, t":

Example:

$str1 = "2,161,0,0"; 
$str2 = "2,3,10,4";

for $str1 we have to recover:

2 -> x

161 -> y

0 -> z

0 -> t
sepehr
  • 17,110
  • 7
  • 81
  • 119
yannick
  • 52
  • 2

3 Answers3

1

Try this:

explode(',', $str1);

More on explode: http://php.net/manual/en/function.explode.php

Jules R
  • 553
  • 2
  • 18
1

Since you need the string components as separate variables, which is well, weird, you also need to make use of list as well:

list($x, $y, $z, $t) = explode(',', $str1);

Or using the short list syntax since PHP 7.1:

[$x, $y, $z, $t] = explode(',', $str1);
sepehr
  • 17,110
  • 7
  • 81
  • 119
1

You can follow this example:

$str1 = "2,161,0,0"; 

$array = explode(',', $str1);
print_r($array);

Output will then be:

array
(
    [0] => 2
    [1] => 161
    [2] => 0
    [3] => 0
)

Read more about explode() here.

Sanguinary
  • 354
  • 2
  • 3
  • 16