Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation fgetcsv() splitting at enclosed delimiters

    I have not upgraded or changed PHP since I last used the function fgetcsv(), however it is now breaking up enclosed delimiters.

    Take this for example:
    Code:
    12345,Yes,"Blah blah blah, yadda yadda yadda, something something something",50
    When the following code is called it should produce 4 elements of data in an array but instead it now produces 6:
    PHP Code:
    $fp fopen($source_file'r');
    $field_length 4096;
    $delimiter ',';
    $enclosure '"';

    $data fgetcsv($fp$field_length$delimiter$enclosure); 
    Why is this happening? Why would it suddenly begin happening without altering any code? Could a Windows update have caused this? That is the only thing that changed on the server.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,080
    Thanks
    2
    Thanked 321 Times in 313 Posts
    Are you sure the file still has double-quotes in it? Is the file still saved as an ANSI/ASCII file?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes, it has the quotes. I just checked the file again and the answer to the problem immediately appeared. Inside the quote enclosed fields were doubled-double quotes. This is fine because it escapes the double quotes to a single double quote. Little did I realize but the first doubled-double quote has a back slash in front of it which was confusing the parser. Removing that fixed the issue.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •