I have the following PHP code and would like to split the string into separate variables:
$names = "login1,login2,login3,login4,...loginN";
Example result:
$login1 = "login1";
$login2 = "login2";
$login3 = "login3";
$login4 = "login4";
I have the following PHP code and would like to split the string into separate variables:
$names = "login1,login2,login3,login4,...loginN";
Example result:
$login1 = "login1";
$login2 = "login2";
$login3 = "login3";
$login4 = "login4";
foreach (explode(',', $names) as $name) {
$$name = $name;
}
But really, why in the world do you want to do this?
foreach! In my example I'm showing you how to use it. Just echo $name instead of assigning it.If a more flexible extraction was needed then you could utilize regular expressions via preg_split():
foreach(preg_split('/,/', $names) as $login){
$$login = $login;
};