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 962415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:30:10+00:00 2026-05-16T01:30:10+00:00

I am trying to figure out a way to read a CSV with returns

  • 0

I am trying to figure out a way to read a CSV with returns in it in PHP. The problem is when you read the file like this:

if (($handle = fopen($file, "r")) !== FALSE) {
 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        row data...
    }
}

If you have a retun in the CSV it does not work, it just sees the returns as a new row.

My idea was to have a regex to check for unclosed quotes, but I dont know of anything like that. Any ideas?

  • 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-16T01:30:11+00:00Added an answer on May 16, 2026 at 1:30 am

    Since it seems the built-in fgetcsv does not correctly handle the CSV standard, there are suggestions for alternatives on the PHP man page for fgetcsv – here’s one of them:

    From http://www.php.net/manual/en/function.fgetcsv.php

    The PHP’s CSV handling stuff is
    non-standard and contradicts with
    RFC4180, thus fgetcsv() cannot
    properly deal with files like this
    example …

    There is a quick and dirty
    RFC-compliant realization of CSV
    creation and parsing:

    <?php 
    function array_to_csvstring($items, $CSV_SEPARATOR = ';', $CSV_ENCLOSURE = '"', $CSV_LINEBREAK = "\n") { 
      $string = ''; 
      $o = array(); 
    
      foreach ($items as $item) { 
        if (stripos($item, $CSV_ENCLOSURE) !== false) { 
          $item = str_replace($CSV_ENCLOSURE, $CSV_ENCLOSURE . $CSV_ENCLOSURE, $item); 
        } 
    
        if ((stripos($item, $CSV_SEPARATOR) !== false) 
         || (stripos($item, $CSV_ENCLOSURE) !== false) 
         || (stripos($item, $CSV_LINEBREAK !== false))) { 
          $item = $CSV_ENCLOSURE . $item . $CSV_ENCLOSURE; 
        } 
    
        $o[] = $item; 
      } 
    
      $string = implode($CSV_SEPARATOR, $o) . $CSV_LINEBREAK; 
    
      return $string; 
    } 
    
    function csvstring_to_array(&$string, $CSV_SEPARATOR = ';', $CSV_ENCLOSURE = '"', $CSV_LINEBREAK = "\n") { 
      $o = array(); 
    
      $cnt = strlen($string); 
      $esc = false; 
      $escesc = false; 
      $num = 0; 
      $i = 0; 
      while ($i < $cnt) { 
        $s = $string[$i]; 
    
        if ($s == $CSV_LINEBREAK) { 
          if ($esc) { 
            $o[$num] .= $s; 
          } else { 
            $i++; 
            break; 
          } 
        } elseif ($s == $CSV_SEPARATOR) { 
          if ($esc) { 
            $o[$num] .= $s; 
          } else { 
            $num++; 
            $esc = false; 
            $escesc = false; 
          } 
        } elseif ($s == $CSV_ENCLOSURE) { 
          if ($escesc) { 
            $o[$num] .= $CSV_ENCLOSURE; 
            $escesc = false; 
          } 
    
          if ($esc) { 
            $esc = false; 
            $escesc = true; 
          } else { 
            $esc = true; 
            $escesc = false; 
          } 
        } else { 
          if ($escesc) { 
            $o[$num] .= $CSV_ENCLOSURE; 
            $escesc = false; 
          } 
    
          $o[$num] .= $s; 
        } 
    
        $i++; 
      } 
    
    //  $string = substr($string, $i); 
    
      return $o; 
    } 
    ?> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out a way to read GET method information from a
I am trying to figure out the best way to go about my design
I'm trying to figure out which type of seo i will use for my
I'm trying to figure out how to best keep revision/history information on revisions to
I am new to SQL Server Full Text Searching, and am trying to figure
I have a .sql file containing thousands of individual insert statements. It takes forever
I'm writing a python applet for data acquisition from scientific instruments and I'm trying
I'm using python and psycopg2 to remotely query some psql databases, and I'm trying
I've been working on a class assignment for C++ and we're required to acquire
I want to display about one or two full screens worth of text in

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.