简体   繁体   中英

Get string after '|' character

My String is

$var = 0|var|var1;

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

$var1 = 0;
$var2 = var;
$var3 = var1;

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM