Question

i have an the following string from my database:

";5;78;27;56;66;71;"

how can I extract / explode into variables

$a = "5";
$b = "78";
$c = "27";

thank you for your help!

Was it helpful?

Solution

Reference: http://us1.php.net/explode

$data = ";5;78;27;56;66;71;";
$dataArr = explode(';',$data);
for($i = 0; $i < count($dataArr); $i++){
 ${'var'.$i} = $dataArr[$i];
}

Explode should return an array of values. Then you iterate through said values and dynamically assign a name to each one.

In my example code, you'll get $var1, $var2, $var3 and so on.

OTHER TIPS

array_filter( explode(';', ";5;78;27;56;66;71;") );

The filter is to get rid of some empty values.

Gives you an array of the values:

Array ( [1] => 5 [2] => 78 [3] => 27 [4] => 56 [5] => 66 [6] => 71 )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top