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
    643

    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,206
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

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

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    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
    643
    Unicode (non utf-8)

  6. #6
    Join Date
    Sep 2006
    Posts
    643

    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,206
    And what is it you want done with the CSV file that has no Comma Separation?
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

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

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    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