handling multibyte csv files in php

Download Handling multibyte CSV files in PHP

If you can't read please download the document

Upload: danielrhodes

Post on 24-May-2015

2.792 views

Category:

Technology


3 download

DESCRIPTION

Handling multibyte CSV files in PHP using fgetcsv() and setlocale()

TRANSCRIPT

  • 1. Handling multibyte CSV files in PHP using fgetcsv() and setlocale() By Daniel Rhodes of Warp Asylum ( www.warpasylum.co.uk )

2. fgetcsv()

  • fgetcsv() Gets line from file pointer and parse for CSV fields

3. There is no mb_fgetcsv() ! 4. But Locale setting is taken into account by fgetcsv() 5. Let's see what we can do... 6. A case study

  • Let's say that we've got the following all on UTF-8:
  • Mbstring.internal_encoding

7. Our database 8. Our PHP files 9. Our HTML output 10. But we need to process the following CSV file which is in EUC-JP encoding... 11. The CSV file 12. The CSV filesource 13. The first attempt

  • Well, let's try to process it as we would a normal non-multibyte CSV file...

14. The first attempt 15. Let's try again

  • Doing nothing clearly didn't work!

16. Let's try setting mbstring.internal_encoding to that of our CSV file, EUC-JP... 17. Let's try again 18. Using setlocale()

  • Mbstring.internal_encoding clearly didn't work!

19. Let's try using the setlocale() function... 20. Using setlocale() 21. Success!

  • Setlocale() is the key!

22. Just out of curiousity, let's remove the Japanese directive settings for mbstring and see if it still works... 23. Out of curiousity... 24. Solution

  • Setlocale() is the key

25. Set locale to that of CSV file 26. Note that setlocale() doesn'tpermanentlyaffect the system locale setting 27. Mbstring settings not important 28. Mbstring itself not actually needed! 29. That's all folks!

  • I'll leave you with some things to think about:
  • Locale:: class (from PHP 5.3) might also be of use

30. Specified locale for setlocale() must be supported by OS of PHP server 31. Quoting string fields in the source CSV file may help enormously so much so that setlocale() is not needed! 32. Questions welcome at [email protected]