Skip to content
Advertisement

How to keep only a part of an explode of a csv column in php?

I have this php code that allows me to read a csv file and add 10 columns at the end to decompose the column $data[23].

This column contains for example :

M6-Min Ord Qty 6,GO-Good Support,RP-Removable Padding
M6-Min Ord Qty 6
M6

I would like these lines there:

PICT01 PICT02 PICT03 PICT04 PICT05
M6      GO     RP
M6
M6

I would like to display and keep only the part to the left of the dash. For the moment my code works but just separates this information on all the columns, how can I have only the part to the left of the dash?

<?php
function multiexplode ($delimiters,$string) {

            $ready = str_replace($delimiters, $delimiters[0], $string);
            $launch = explode($delimiters[0], $ready);
            return  $launch;
        }

//Modifications on csv file
$delimiter = ";"; 
$csv_data = array();
$row = 1;
if (($handle = fopen($nomcsv, 'r')) !== FALSE) {
    while (($data = fgetcsv($handle, 10000, $delimiter)) !== FALSE) {
        
        //Add columns at the end
        $data['Pictures Names'] = (!empty($data[4]) ? ($data[7] ?: '') . "_" . $data[4] . '.jpg' : ''); 
        
        
        $exploded = multiexplode(array(",","-"),$data[23]);
        
        $data['PICT01'] = $exploded[0];
        $data['PICT02'] = $exploded[1];  
        $data['PICT03'] = $exploded[2];  
        $data['PICT04'] = $exploded[3];  
        $data['PICT05'] = $data[23];  
        $data['PICT06'] = $data[23];  
        $data['PICT07'] = $data[23];  
        $data['PICT08'] = $data[23];  
        $data['PICT09'] = $data[23];  
        $data['PICT010'] = $data[23];  


        //Modifications on the fourth line
        if ($row == 4) 
        {
               //All uppercase
               $data = array_map('strtoupper', $data);  
               $data = str_replace(' *', '', $data);
               $data = str_replace('/', '', $data);                             
        }       
            $csv_data[] = $data; 
                              
        $row++;      
    }
    fclose($handle);
}
?>

Advertisement

Answer

To process a single row, you need to first split the string by comma. Then you need to take each result of that split separately, and split it by dash. Finally, add the first item from each of those dash splits to your output – they will be the strings you are looking for.

e.g. here’s a function which would split a single row as per your sample data:

function multiSplit($string)
{
    $output = array();
    $cols = explode(",", $string);

    foreach ($cols as $col)
    {
        $dashcols = explode("-", $col);
        $output[] = $dashcols[0];
    }
    
    return $output;
}

Example of usage:

$row = "M6-Min Ord Qty 6,GO-Good Support,RP-Removable Padding";
$out = multiSplit($row);
var_dump($out);

Live demo: http://sandbox.onlinephpfunctions.com/code/b1b7b60da02b9ef13c5747414016aa0fcf296249

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement