Guest User

Untitled

a guest
Apr 26th, 2018
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. $delimitador = ';';
  2. $cerca = '"';
  3.  
  4. // Abrir arquivo para leitura
  5. $f = fopen($_FILES['uploadChange']['tmp_name'], 'r');
  6. if ($f) {
  7.  
  8. // Ler cabecalho do arquivo
  9. $cabecalho = fgetcsv($f, 0, $delimitador, $cerca);
  10.  
  11. // Enquanto nao terminar o arquivo
  12. while (!feof($f)) {
  13.  
  14. // Ler uma linha do arquivo
  15. $linha = fgetcsv($f, 0, $delimitador, $cerca);
  16. if (!$linha) {
  17. continue;
  18. }
  19.  
  20. // Montar registro com valores indexados pelo cabecalho
  21. $registro = array_combine($cabecalho, $linha);
  22.  
  23. echo "<pre>";
  24. print_r($registro);
  25. echo "</pre>";
  26.  
  27. }
  28. fclose($f);
  29. }
  30.  
  31. Array
  32. (
  33. [Nome] => Andr� Baill
  34. [Idade] => 29
  35. [telefone] => (41) 9 9647-9364
  36. [email] => srandrebaill@gmail.com
  37. )
  38.  
  39. foreach($linha as $key => $value)
  40. {
  41. $linha[$key] = utf8_encode($value);
  42. }
  43.  
  44. if (!$linha) {
  45. continue;
  46. }
  47.  
  48. function is_utf8($str)
  49. {
  50. $c=0; $b=0;
  51. $bits=0;
  52. $len=strlen($str);
  53. for($i=0; $i<$len; $i++){
  54. $c=ord($str[$i]);
  55. if($c > 128){
  56. if(($c >= 254)) return false;
  57. elseif($c >= 252) $bits=6;
  58. elseif($c >= 248) $bits=5;
  59. elseif($c >= 240) $bits=4;
  60. elseif($c >= 224) $bits=3;
  61. elseif($c >= 192) $bits=2;
  62. else return false;
  63. if(($i+$bits) > $len) return false;
  64. while($bits > 1){
  65. $i++;
  66. $b=ord($str[$i]);
  67. if($b < 128 || $b > 191) return false;
  68. $bits--;
  69. }
  70. }
  71. }
  72. return true;
  73. }
  74.  
  75. <?php
  76.  
  77. function is_utf8($str)
  78. {
  79. $c=0; $b=0;
  80. $bits=0;
  81. $len=strlen($str);
  82. for($i=0; $i<$len; $i++){
  83. $c=ord($str[$i]);
  84. if($c > 128){
  85. if(($c >= 254)) return false;
  86. elseif($c >= 252) $bits=6;
  87. elseif($c >= 248) $bits=5;
  88. elseif($c >= 240) $bits=4;
  89. elseif($c >= 224) $bits=3;
  90. elseif($c >= 192) $bits=2;
  91. else return false;
  92. if(($i+$bits) > $len) return false;
  93. while($bits > 1){
  94. $i++;
  95. $b=ord($str[$i]);
  96. if($b < 128 || $b > 191) return false;
  97. $bits--;
  98. }
  99. }
  100. }
  101. return true;
  102. }
  103.  
  104.  
  105. $delimitador = ';';
  106. $cerca = '"';
  107.  
  108. // Abrir arquivo para leitura
  109. $f = fopen('arquivo.csv', 'r');
  110. if ($f) {
  111.  
  112. // Ler cabecalho do arquivo
  113. $cabecalho = fgetcsv($f, 0, $delimitador, $cerca);
  114.  
  115. // Enquanto nao terminar o arquivo
  116. while (!feof($f)) {
  117.  
  118. // Ler uma linha do arquivo
  119. $linha = fgetcsv($f, 0, $delimitador, $cerca);
  120. if (!$linha) {
  121. continue;
  122. }
  123.  
  124. foreach($linha as $key => $value)
  125. {
  126. if(!is_utf8($value))
  127. {
  128. $linha[$key] = utf8_encode($value);
  129. }
  130. }
  131.  
  132. // Montar registro com valores indexados pelo cabecalho
  133. $registro = array_combine($cabecalho, $linha);
  134.  
  135. echo "<pre>";
  136. var_dump($registro);
  137. echo "</pre>";
  138.  
  139. }
  140. fclose($f);
  141. }
  142.  
  143. NOME;EMAIL
  144. "André Baill";"srandrebaill@gmail.com"
  145. "João";"joãozito@mail.com"
Add Comment
Please, Sign In to add comment