K.Nagare
K.Nagare

Reputation: 19

How to store each output line of fgets() into separate variable?

fgets() shows output is as below =

string(10) "1236457" string(8) "xyz" string(15) "credit:87845 " string(17) "balance:644546 " string(17) "remaining:7463847"

i want to store $var1=1236457 $var2= xyz and same for all.But without using explode.

Upvotes: 0

Views: 105

Answers (1)

Andreas
Andreas

Reputation: 23958

If you insist on having variables you can iterate the array and create associative keys and then use extract to create the variables.
In this example I use 0 indexed lines, meaning first line is $line0.

$arr = explode("\n", $str);
Foreach($arr as $key => $val){
   $new["line" . $key] = $val;
}

Extract($new);

Echo $line2;

https://3v4l.org/psh6H

Upvotes: 1

Related Questions