i want to import a file and then use the items in variables
i have the file imported i also turn it into chunks with explode
now i tried preg_replace to get rid of certain charaters i need to get rid of " character from the file then split it by , and ideas or a kick in the right direction would be appreciated
i also tryed to preg by , but still cnat get it right

my code now

//==========================================================================================================
// READ CVS FILE (GET DATA FROM FILE)
//==========================================================================================================

$myFile = "products.csv";
$fh = fopen($myFile, 'r');
$theStuff = fread($fh, filesize($myFile));
fclose($fh);
//echo $theStuff;


//==========================================================================================================
// explode data into arrays
//==========================================================================================================
//preg_replace(',', "", $thestuff);

$chunks = explode(',', $theStuff);
echo $chunks[1];

Dani AI

Generated

Short answer: stop trying to strip quotes with regex and splitting the whole file by commas. CSV uses quoting to allow commas (and even newlines) inside fields, so blindly removing " or exploding on , will break many valid rows. was right to point toward the CSV parser functions built into PHP — they handle enclosures, escapes and multiline fields reliably.

A practical pattern: stream the file with fopen() and parse each line with fgetcsv(). If the first line is a header, map it into associative rows with array_combine() so you can reference columns by name. fgetcsv() accepts delimiter/enclosure/escape parameters if your file uses something other than the default comma and double-quote.

$handle = fopen('products.csv', 'r');
if ($handle) {
    $header = fgetcsv($handle, 0, ',', '"');
    while (($row = fgetcsv($handle, 0, ',', '"')) !== false) {
        if ($header) {
            $record = array_combine($header, $row); // verify counts match
            // use $record['ColumnName']
        } else {
            // use $row[0], $row[1], ...
        }
    }
    fclose($handle);
}

Troubleshooting tips: if the CSV has a UTF-8 BOM, strip it from the first header field before using it. If you already have the whole file as a string, str_getcsv() can parse lines, but splitting by newline first will break quoted fields that contain newlines — another reason to prefer fgetcsv(). Only remove enclosing quotes after parsing (e.g., trim($value, "\"")) if you truly need to, but normally fgetcsv() returns fields without the surrounding quotes. If things still look wrong, paste a representative sample row (including quotes) so the parsing expectations can be checked.

Recommended Answers

All 2 Replies

suprisingly i did not find that in my travels of the world wide web thanks for the reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.