If I have a text file:
Ramon,19,Libra
Renata,25,Aries
Roxy,52,Leo
If I am to create an array:
print_r(explode(",", $str));
How do I account for the newlines?
If I have a text file:
Ramon,19,Libra
Renata,25,Aries
Roxy,52,Leo
If I am to create an array:
print_r(explode(",", $str));
How do I account for the newlines?
Depends on what you want, but try file()
:
$lines = file('/path/to/file.txt');
foreach($lines as $line) {
$data[] = explode(',', $line);
}
print_r($data);
Another good way is use str_getcsv
foreach(file('/path/to/file.txt') as $line) {
$data[] = str_getcsv($line);
}
var_dump($data);
And returns:
Array
(
[0] => Array
(
[0] => Ramon
[1] => 19
[2] => Libra
)
[1] => Array
(
[0] => Renata
[1] => 25
[2] => Aries
)
[2] => Array
(
[0] => Roxy
[1] => 52
[2] => Leo
)
)
Or you can use the fgetcsv function
just another way it could be done
$filestring = file_get_contents('...path to file...');
$lines = explode(PHP_EOL,$filestring);
foreach($lines as $line) {
$data[] = explode(',', $line);
}
print_r($data);