The example of fgetcsv() function in PHP.
The example of fgetcsv() function in PHP.
Examples of fgetcsv() function in php
Description
The PHP fgetcsv() function is used to parse the CVS file in PHP program. Here you will see how you can do so. We need a CSV file and then after opening the file we can parse using the fgetcsv() function. Here is the details of fgetcsv() function.
Syntax of fgetcsv() Funtion in PHP Program
Array fgetcsv ( file_handler [, $length [, $delimiter [, $enclosure [, string $escape ]]]] )
CSV is a type of file. Its full form is Comma Separated Values.
In Excel files can be save as .csv.
These files opened in any text editor shows values as separated with comma.
In PHP csv file can be read and displayed the values using fgetcsv() function.
It can be achieve by using an file handler returned by fopen(), popen(), or fsockopen()) it parses each line with the CSV fields.
It returns the CSV fields in an array with each line.
Each array is containing the fields of each line.
It returns FALSE on failure and EOF.
Code for PHP fgetcsv() Function in PHP
<?php
$file1=fopen("c:/rose1/rose2.csv","r"); //Open the CSV file
while(($array1=fgetcsv($file1,1000,","))!==FALSE) //parse file in loop
{
for($x=0;$x<sizeof($array1);$x++)
echo " ".$array1[$x];
if ($x%2==0)
echo "<br/>";
}
?>
<?php
$file1=fopen("c:/rose1/rose2.csv","r");
while(($array1=fgetcsv($file1))!==FALSE)
{
foreach ($array1 as $key=>$value)
echo " ".$value;
echo "<br/>";
}
?>
Output
roll name
1 ram
2 shyam
3 mohan
4 sita