Steps for Parsing CSV file using PHP
Step 1. Create a folder and add that CSV file and create a new PHP file in it.
Step 2. Open the PHP file and write the following code in it which is explained in the following steps.
- Open dataset of CSV using fopen function.
$open = fopen("filename.csv", "r");
- Read a line using fgetcsv() function.
$data = fgetcsv($open, 1000, ",");
- Use a loop to iterate in every row of data.
while (($data = fgetcsv($open, 1000, ",")) !== FALSE)
{
// Read the data
}
- Close that file using PHP fclose() method.
fclose($open);
How to parse a CSV File in PHP ?
In this article, we learn to parse a CSV file using PHP code, along with understanding its basic implementation through examples.
Approach: The following approach will be utilized to parse the CSV File using PHP, which is described below:
Step 1. Add data to an Excel file. The following example is given for sample data having Name and Coding Score as their column headers.
Step 2. Convert to CSV file by following the path. Go to File>Export>Change File Type> CSV Type. Save the file in your working folder with the name “Book1.csv”.
Contact Us