csv to array PHP

28.06.2021
<pre><?php $s = file_get_contents('./bd0.csv'); function csv_to_array($string_csv, $delimiter){ $lines = explode("\n", $string_csv); unset($string_csv); $rt = []; $rt_i = 0; for($i = 0, $k = count($lines); $i < $k; $i++){ if(!isset($lines[$i])) continue; $rt[$rt_i] = []; $rt_ar_i = 0; $quotes = 0; for($i1 = 0, $k1 = strlen($lines[$i]); $i1 < $k1; $i1++){ if(!isset($rt[$rt_i][$rt_ar_i])) $rt[$rt_i][$rt_ar_i] = ''; $ch = substr($lines[$i], $i1, 1); if($ch === '"'){ if(substr($lines[$i], $i1+1, 1) === '"'){ $i1++; }else{ $quotes++; continue; } } if($ch === ';' && $quotes % 2 === 0){ $rt_ar_i++; continue; } $rt[$rt_i][$rt_ar_i] .= $ch; } $rt_i++; } return $rt; } print_r(csv_to_array($s, ';'));

Дополнительно

Для тех, кто возится с 1С
function txt1c_to_array($s){ $rt = []; $lines = explode("\n", $s); for($i = 0, $k = count($lines); $i < $k; $i++){ if(!isset($lines[$i])) continue; array_push($rt, explode("\t", $lines[$i])); } return $rt; } function clear1($s){ $s = str_replace(['? ', '—', '-', 'Ø ', '⌀ ', //, '*', '=', ',', '.' ], '', $s); $s = trim($s); return $s; }