PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/accounts/application/libraries/csvreader.php

https://github.com/narendranag/Snippets
PHP | 122 lines | 58 code | 8 blank | 56 comment | 12 complexity | d57fafdd5f97c31e6d2ecc95126f0e2f MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CSVReader Class
  4. *
  5. * $Id: csvreader.php 147 2007-07-09 23:12:45Z Pierre-Jean $
  6. *
  7. * Allows to retrieve a CSV file content as a two dimensional array.
  8. * The first text line shall contains the column names.
  9. *
  10. * Let's consider the following CSV formatted data:
  11. *
  12. * col1;col2;col3
  13. * 11;12;13
  14. * 21;22;23
  15. *
  16. * It's returned as follow by the parsing operations:
  17. *
  18. * Array(
  19. * [0] => Array(
  20. * [col1] => 11,
  21. * [col2] => 12,
  22. * [col3] => 13
  23. * )
  24. * [1] => Array(
  25. * [col1] => 21,
  26. * [col2] => 22,
  27. * [col3] => 23
  28. * )
  29. * )
  30. *
  31. * @author Pierre-Jean Turpeau
  32. * @link http://www.codeigniter.com/wiki/CSVReader
  33. */
  34. class CSVReader {
  35. var $fields; /** columns names retrieved after parsing */
  36. var $separator = ';'; /** separator used to explode each line */
  37. /**
  38. * Parse a text containing CSV formatted data.
  39. *
  40. * @access public
  41. * @param string
  42. * @return array
  43. */
  44. function parse_text($p_Text) {
  45. $lines = explode("\n", $p_Text);
  46. return $this->parse_lines($lines);
  47. }
  48. /**
  49. * Parse a file containing CSV formatted data.
  50. *
  51. * @access public
  52. * @param string
  53. * @return array
  54. */
  55. function parse_file($p_Filepath) {
  56. $lines = file($p_Filepath);
  57. return $this->parse_lines($lines);
  58. }
  59. /**
  60. * Parse an array of text lines containing CSV formatted data.
  61. *
  62. * @access public
  63. * @param array
  64. * @return array
  65. */
  66. function parse_lines($p_CSVLines) {
  67. $content = FALSE;
  68. foreach( $p_CSVLines as $line_num => $line ) {
  69. if( $line != '' ) { // skip empty lines
  70. $elements = split($this->separator, $line);
  71. if( !is_array($content) ) { // the first line contains fields names
  72. $this->fields = $elements;
  73. $content = array();
  74. } else {
  75. $item = array();
  76. foreach( $this->fields as $id => $field ) {
  77. if( isset($elements[$id]) ) {
  78. $item[$field] = $elements[$id];
  79. }
  80. }
  81. $content[] = $item;
  82. }
  83. }
  84. }
  85. return $content;
  86. }
  87. // Added by NAG
  88. function get_csv_values($string, $separator=",")
  89. {
  90. $elements = explode($separator, $string);
  91. for ($i = 0; $i < count($elements); $i++) {
  92. $nquotes = substr_count($elements[$i], '"');
  93. if ($nquotes %2 == 1) {
  94. for ($j = $i+1; $j < count($elements); $j++) {
  95. if (substr_count($elements[$j], '"') > 0) {
  96. // Put the quoted string's pieces back together again
  97. array_splice($elements, $i, $j-$i+1,
  98. implode($separator, array_slice($elements, $i, $j-$i+1)));
  99. break;
  100. }
  101. }
  102. }
  103. if ($nquotes > 0) {
  104. // Remove first and last quotes, then merge pairs of quotes
  105. $qstr =& $elements[$i];
  106. $qstr = substr_replace($qstr, '', strpos($qstr, '"'), 1);
  107. $qstr = substr_replace($qstr, '', strrpos($qstr, '"'), 1);
  108. $qstr = str_replace('""', '"', $qstr);
  109. }
  110. }
  111. return $elements;
  112. }
  113. }
  114. ?>