xxxxxxxxxx
$file = "file.csv";
$csv = new SplFileObject($file);
$csv -> setFlags(SplFileObject::READ_CSV);
$csv -> setCsvControl(';'); //separator change if you need
foreach( $csv as $ligne){
print_r($ligne); //$ligne is an array
}
xxxxxxxxxx
$csvFile = file('../somefile.csv');
$data = [];
foreach ($csvFile as $line) {
$data[] = str_getcsv($line);
}
xxxxxxxxxx
<?php
if (($open = fopen("Book1.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($open, 1000, ",")) !== FALSE)
{
$array[] = $data;
}
fclose($open);
}
echo "<pre>";
//To display array data
var_dump($array);
echo "</pre>";
xxxxxxxxxx
<?php
$CSVfp = fopen("fruits.csv", "r");
if ($CSVfp !== FALSE) {
while (! feof($CSVfp)) {
$data = fgetcsv($CSVfp, 1000, ",");
print_r($data);
}
}
fclose($CSVfp);
?>
xxxxxxxxxx
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
xxxxxxxxxx
<?php
ini_set('auto_detect_line_endings',TRUE);
$handle = fopen('/path/to/file','r');
while ( ($data = fgetcsv($handle) ) !== FALSE ) {
//process the array in $data
var_dump($data);
}
ini_set('auto_detect_line_endings',FALSE);
xxxxxxxxxx
<?php
$CSVfp = fopen("fruits.csv", "r");
if ($CSVfp !== FALSE) {
?>
<div class="phppot-container">
<table class="striped">
<thead>
<tr>
<th>NAME</th>
<th>COLOR</th>
</tr>
</thead>
<?php
while (! feof($CSVfp)) {
$data = fgetcsv($CSVfp, 1000, ",");
if (! empty($data)) {
?>
<tr class="data">
<td><?php echo $data[0]; ?></td>
<td><div class="property-display"
style="background-color: <?php echo $data[2]?>;"><?php echo $data[1]; ?></div></td>
</tr>
<?php }?>
<?php
}
?>
</table>
</div>
<?php
}
fclose($CSVfp);
?>
xxxxxxxxxx
fgetcsv ( resource $stream , int $length = 0 , string $separator = "," , string $enclosure = '"' , string $escape = "\\" ) : array
Code language: PHP (php)