dcsimg
www.webdeveloper.com
Results 1 to 9 of 9

Thread: Question Converting a csv file to utf-8

  1. #1
    Join Date
    Sep 2006
    Posts
    656

    Question Question Converting a csv file to utf-8

    Hi All

    I am using the fopen and fgets function to open a csv file loop through the first few records and perform certain task...the problem is when my php script tries to open a non-utf-8 csv, it fails to read it...but if I convert that csv (using an editor) to utf-8 format then my php script works fine.

    So what I was wondering is if there is anyway in php to check if a file is non utf-8, if so then convert the file to utf-8 so that I do not have to manually convert the character set.


    Thanks for any inputs.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,591
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Sep 2006
    Posts
    656
    No it does not help me convert the entire file to utf-8

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,693
    Out of curiosity, what encoding is it in before you convert it?

    In any case, sounds like a job for the multi-byte string functions.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Sep 2006
    Posts
    656
    Unicode (non utf-8)

  6. #6
    Join Date
    Sep 2006
    Posts
    656

    Question

    The mb string function is unable to detect for a given non unicode string in a csv file.

    PFA csv file

    Can u pls help?


    Thanks
    Attached Files Attached Files

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,591
    And what is it you want done with the CSV file that has no Comma Separation?
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  8. #8
    Join Date
    Sep 2006
    Posts
    656
    Well I am handling that case internally in my php script.

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,693
    I don't have time to try it right now, but my guess (and it's really just a guess) is that you may have to read the entire file into a string variable (e.g. with file_get_contents()), process that variable through the MB functions (so that any BOM or similar indicators at the beginning of the file are picked up), then save the converted string as a temporary file that you can then process with fgetcsv().
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles