0

I am getting the contents of a CSV file and displaying (it works).

if (($handle = fopen($url, 'r')) === false) {
    die('Error opening file');
}


$headers = fgetcsv($handle, 1024, ',');
$complete = array();
while ($row = fgetcsv($handle, 1024, ',')) {
    $complete[] = array_combine($headers, $row);
}
fclose($handle);

However, in this CSV file there is a field that has contents for example like this:

"123456,123456,123456,123456"

I think my code isn't processing because of the double quotes, I think I need to convert to single quotes. If thats the case how would I integrate the following (I was thinking something like):

str_replace('"',"'", $url);
0

1 Answer 1

2

Look at the other parameters for fgetcsv()

By default the enclosure character is set to ", which means anything between quotes should be considered a single value. Replace that parameter with what you actually use as the enclosure character in the csv and it will work.

Something like (if your enclosure character is '):

while ($row = fgetcsv($handle, 1024, ',', "'")) {

Better than to read it wrong and try to fix it afterwards with str_replace.

Sign up to request clarification or add additional context in comments.

1 Comment

hi,thanks for taking the time - it looks like it's working! is there a quick way to only include the header and the next row?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.