Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 713283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:56:37+00:00 2026-05-14T04:56:37+00:00

I have a CSV file that I’m working with, and all the fields are

  • 0

I have a CSV file that I’m working with, and all the fields are comma separated. But some of the fields themselves, contain commas. In the raw CSV file, the fields that contain commas, are encapsulated with quotes, as seen here;

"Doctor Such and Such, Medical Center","555 Scruff McGruff, Suite 103, Chicago IL 60652",(555) 555-5555,,,,something else

the code I’m using is below

<?PHP
    $file_handle = fopen("file.csv", "r");
    $i=0;
    while (!feof($file_handle) ) {
        $line = fgetcsv($file_handle, 1024);
        $c=0;
        foreach($line AS $key=>$value){
            if($i != 0){
                if($c == 0){
                    echo "[ROW $i][COL $c] - $value"; //First field in row, show row #
                }else{
                    echo "[COL $c] - $value"; // Remaining fields in row
                }
            }
            $c++;
        }
        echo "<br>"; // Line Break to next line
        $i++;
    }
    fclose($file_handle);
?>

The problem is I’m getting the fields with the commas split into two fields, which messes up the number of columns I’m supposed to have.

Is there any way I could search for commas within quotes and convert them, or another way to deal with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:56:38+00:00Added an answer on May 14, 2026 at 4:56 am

    You can use the “enclosure” parameter. See the fgetcsv documentation.

    $handle = fopen("file", "r");
    if ($handle ) {
        while (($line = fgetcsv($handle, 2048, ",", '"')) !== FALSE) {
          print_r( $line)."\n";
        }
        fclose($handle);
    }
    

    Output:

    $ cat file
    "Doctor Such and Such, Medical Center","555 Scruff McGruff, Suite 103, Chicago IL 60652",(555) 555-5555,,,,something else
    
    $ php test.php
    Array
    (
        [0] => Doctor Such and Such, Medical Center
        [1] => 555 Scruff McGruff, Suite 103, Chicago IL 60652
        [2] => (555) 555-5555
        [3] =>
        [4] =>
        [5] =>
        [6] => something else
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CSV file that has lists of values for some fields. They're
I have a csv file that has 2 columns separated by a comma -
I have .csv file that contain 2 columns delimited with , . file.csv word1,word2
I have a CSV file that has rows, where data for some columns only
I have a csv file that is comma delimited. I have no problem with
So I have a CSV file that looks like this: 12345, Here is some
I have a CSV file that has only 1 column, but has close to
I have a CSV file that has 2999 rows but on importing it to
I have a CSV file that goes something like this: ['Name1', '', '', '',
I have a CSV file that is formatted like: 0.0023709,8.5752e-007,4.847e-008 and I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.