Skip Navigation

Basic parse address from csv with PHP

Uses: https://github.com/rap2hpoutre/fast-excel

Runs way faster then Laravel Excel and is easier to use for smaller things like this.

use Rap2hpoutre\FastExcel\FastExcel;

$rows = (new FastExcel)->import(storage_path('files/data.csv'));

$newData = [];
foreach($rows as $row){
  $name =  $row['Name'];
  $address = preg_split("/\r\n|\n|\r/",$row['Address']); 
  $address_line_1 = $address[0];
  $address_line_2 = $address[1];
  $address_line_3 = (isset($address[2]) ? $address[2] : '');
  
  array_push($newData, [ $name, $address_line_1, $address_line_2,  $address_line_3 ] );
}

// export the data
(new FastExcel($newData))->export('file.csv');

Related Snippets

See all