Read and write CSV file in PHP with different Unicode in UTF-8 format. Reading a CSV file in PHP is very easy by fgetcsv function or by fopen a file to read line by line code in PHP but these functions do not work for different Unicode format.
A few days ago, I created a web application where user can import CSV file and data was being inserted into the database but some users were uploading CSV file in a different format other than UTF-8 then both of the above-mentioned function was not working properly.
Let’s understand what problem was occurring? The main problem was occurred to read each cell value (row and column value) because when we read the row and column value then it was giving with some special character by adding some extra spaces between each character which is not manageable without converting to UTF-8. I had implemented different encoding functions to prevent this issue but always new issue occurs.
After that, I researched this topic and found a library that is very easy to implement in our application. I am going to share the library details so that you can easily read and write CSV files in PHP with all Unicode formats.
Read and Write CSV file in PHP by ParseCsv library
I am going to share some details about the ParseCsv library. This library was created by Jimeh in early 2007 due to the lack of built-in and third-party support for handling CSV data in PHP.
According to the ParseCsv definition on GitHub “ParseCsv is an easy-to-use PHP class that reads and writes CSV data properly. It fully conforms to the specifications outlined in the Wikipedia article (and thus RFC 4180). It has many advanced features which help make your life easier when dealing with CSV data.“
Features of ParseCsv library:
- This is a complete CSV solution in PHP.
- Error detection for incorrectly formatted input. It attempts to be intelligent, but can not be trusted 100% due to the structure of CSV, and how different programs like Excel for example outputs CSV data.
- It supports PHP 5.5 and higher version. I have tested this with the 7.3 version and it works perfectly.
- It automatically converted each encoding format CSV file to UTF-8 so that you can work easily with your application.
- It also solves the indexing issue at the time of reading CSV files.
- It also gives header text in the different index and data text in the different index so that you can verify your header and data.
How to use the ParseCsv library in PHP?
To use this library you have to install it via Command prompt if you using Windows operating system and via Composer and others which supported by another operating system or another framework Laravel.
Installation via Composer
composer require parsecsv/php-parsecsv
If you are using CodeIgniter 3 Framework or Core PHP version then you can also download code by the given link and place this in a particular folder. For CodeIgniter 3, you can put your code in either the third_party folder or the libraries folder. Download Library Other Download Source
If you don’t use a framework such as Drupal, Laravel, Symfony, Yii, etc., you may have to manually include Composer’s autoloader file in your PHP script:
require_once __DIR__ . '/vendor/autoload.php';
On GitHub, the ParseCsv developer mentioned different techniques to read and write files but here, I am going to share my own code to read different Unicode files. The below code is very helpful to read either UTF-8 files or other encoding formats like UCS-2 LE BOM, UCS-2 BE BOM, UTF-8-BOM, ANSI, and other encoding formats.
<?php
require_once (APPPATH . 'third_party/csv_reader/vendor/autoload.php'); #change this path
/**
* function helps to get csv data in array format from a CSV file
* This function automatically gets all encoded type data in UTF-8
* @param: string $fileName [file name with path]
* @param: boolean $onlyHeading [Pass true if you want to get only heading and false for all data with heading]
* @return array
*/
if(!function_exists('getCSVDataByParser')){
function getCSVDataByParser($fileName, $onlyHeading = false){
$csv = new \ParseCsv\Csv();
$csv->use_mb_convert_encoding = true;
$csv->parse($fileName);
$finalData = array();
$finalData[0] = splitSingleStringBySpace($csv->titles);
if(empty($csv->data) || $onlyHeading === true){
return $finalData;
}
$increment = 1;
foreach($csv->data as $value){
$tempArray = array();
foreach($csv->titles as $key => $title){
$tempArray[$key] = $value[$title];
}
$finalData[$increment++] = splitSingleStringBySpace($tempArray);
}
return $finalData;
}
}
/**
* function helps to convert string to array by spliting string with the help of space
* @param array $data
* @return array
*/
function splitSingleStringBySpace($data){
if(count($data) > 1){
return $data;
}
$newData = array();
foreach($data as $key => $singleData){
$newData = preg_split('/\s+/', $singleData);
}
return $newData;
}
You can also customize this function by your own way.
How to call function to read CSV file?
The above code that I have shared with you in the above code snippet is customized by me to make it easy for other developers. To call our function, you have to call by given method:
<?php
$csv = getCSVDataByParser('your file name here with path', true / false);
In the csv variable, you will get an array of each row and column.
Conclusion and Final Words
Reading the CSV file in PHP and in other programming languages and the scripting language is very easy but you have to understand the flow of execution before implementation. Sometimes what happens that when users created CSV file in different operating system then some file type is a little bit different than Unicode that makes an issue like indexing issue.
I always try to make code easier so that you can call my created function and implement into your code to make your development easy.