-2

My String is

$var = 0|var|var1;

i want to save this variable in diffrent variable after | like

$var1 = 0;
$var2 = var;
$var3 = var1;
  • 1
    Does this answer your question? [Split a comma-delimited string into an array?](https://stackoverflow.com/questions/1125730/split-a-comma-delimited-string-into-an-array) – Ivar Jul 05 '21 at 09:32

2 Answers2

0

You can use the explode() function to split the string and then assign it whatever variable you like (or just use the value in the array):

$parts = explode('|', $var); 

$var1 = $parts[0]; 
$var2 = $parts[1]; 
$var3 = $parts[2];

Documentation is here

Markus Müller
  • 2,611
  • 1
  • 17
  • 25
0

There's also a way to get the values straight into variables, without the array.

https://www.php.net/manual/en/function.list.php

list($var1, $var2, $var3) = explode('|', $var);

Or since PHP 7.1 https://www.php.net/manual/en/migration71.new-features.php#migration71.new-features.symmetric-array-destructuring

[$var1, $var2, $var3] = explode('|', $var);
Hendrik
  • 756
  • 6
  • 16