欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php读取csv文件类

程序员文章站 2024-01-15 20:27:34
...
  1. define("CSV_Start", 0);
  2. define("CSV_Quoted", 1);
  3. define("CSV_Quoted2", 2);
  4. define("CSV_Unquoted", 3);
  5. function readCSV($fh, $len, $delimiter = ',', $enclosure = '"') {
  6. $data = Array();
  7. $fildNr = 0;
  8. $state = CSV_Start;
  9. $data[0] = "";
  10. do {
  11. $line = fgets($fh, $len);
  12. for ($ix = 0; $ix if ($line[$ix] == $delimiter) {
  13. if ($state != CSV_Quoted) {
  14. $fildNr++;
  15. $data[$fildNr] = "";
  16. $state = CSV_Start;
  17. } else {
  18. $data[$fildNr] .= $line[$ix];
  19. }
  20. } elseif ($line[$ix] == $enclosure) {
  21. if ($state == CSV_Start) {
  22. $state = CSV_Quoted;
  23. } elseif ($state == CSV_Quoted) {
  24. $state = CSV_Quoted2;
  25. } elseif ($state == CSV_Quoted2) {
  26. $data[$fildNr] .= $line[$ix];
  27. $state = CSV_Quoted;
  28. } else {
  29. $data[$fildNr] .= $line[$ix];
  30. }
  31. } else {
  32. $data[$fildNr] .= $line[$ix];
  33. if ($state == CSV_Quoted2) {
  34. echo "error";
  35. } elseif ($state == CSV_Start) {
  36. $state = CSV_Unquoted;
  37. }
  38. }
  39. }
  40. } while ($state == CSV_Quoted);
  41. return $data;
  42. }
  43. ?>
复制代码

php, csv
相关标签: php读取csv文件类