Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $delimitador = ';';
- $cerca = '"';
- // Abrir arquivo para leitura
- $f = fopen($_FILES['uploadChange']['tmp_name'], 'r');
- if ($f) {
- // Ler cabecalho do arquivo
- $cabecalho = fgetcsv($f, 0, $delimitador, $cerca);
- // Enquanto nao terminar o arquivo
- while (!feof($f)) {
- // Ler uma linha do arquivo
- $linha = fgetcsv($f, 0, $delimitador, $cerca);
- if (!$linha) {
- continue;
- }
- // Montar registro com valores indexados pelo cabecalho
- $registro = array_combine($cabecalho, $linha);
- echo "<pre>";
- print_r($registro);
- echo "</pre>";
- }
- fclose($f);
- }
- Array
- (
- [Nome] => Andr� Baill
- [Idade] => 29
- [telefone] => (41) 9 9647-9364
- [email] => srandrebaill@gmail.com
- )
- foreach($linha as $key => $value)
- {
- $linha[$key] = utf8_encode($value);
- }
- if (!$linha) {
- continue;
- }
- function is_utf8($str)
- {
- $c=0; $b=0;
- $bits=0;
- $len=strlen($str);
- for($i=0; $i<$len; $i++){
- $c=ord($str[$i]);
- if($c > 128){
- if(($c >= 254)) return false;
- elseif($c >= 252) $bits=6;
- elseif($c >= 248) $bits=5;
- elseif($c >= 240) $bits=4;
- elseif($c >= 224) $bits=3;
- elseif($c >= 192) $bits=2;
- else return false;
- if(($i+$bits) > $len) return false;
- while($bits > 1){
- $i++;
- $b=ord($str[$i]);
- if($b < 128 || $b > 191) return false;
- $bits--;
- }
- }
- }
- return true;
- }
- <?php
- function is_utf8($str)
- {
- $c=0; $b=0;
- $bits=0;
- $len=strlen($str);
- for($i=0; $i<$len; $i++){
- $c=ord($str[$i]);
- if($c > 128){
- if(($c >= 254)) return false;
- elseif($c >= 252) $bits=6;
- elseif($c >= 248) $bits=5;
- elseif($c >= 240) $bits=4;
- elseif($c >= 224) $bits=3;
- elseif($c >= 192) $bits=2;
- else return false;
- if(($i+$bits) > $len) return false;
- while($bits > 1){
- $i++;
- $b=ord($str[$i]);
- if($b < 128 || $b > 191) return false;
- $bits--;
- }
- }
- }
- return true;
- }
- $delimitador = ';';
- $cerca = '"';
- // Abrir arquivo para leitura
- $f = fopen('arquivo.csv', 'r');
- if ($f) {
- // Ler cabecalho do arquivo
- $cabecalho = fgetcsv($f, 0, $delimitador, $cerca);
- // Enquanto nao terminar o arquivo
- while (!feof($f)) {
- // Ler uma linha do arquivo
- $linha = fgetcsv($f, 0, $delimitador, $cerca);
- if (!$linha) {
- continue;
- }
- foreach($linha as $key => $value)
- {
- if(!is_utf8($value))
- {
- $linha[$key] = utf8_encode($value);
- }
- }
- // Montar registro com valores indexados pelo cabecalho
- $registro = array_combine($cabecalho, $linha);
- echo "<pre>";
- var_dump($registro);
- echo "</pre>";
- }
- fclose($f);
- }
- NOME;EMAIL
- "André Baill";"srandrebaill@gmail.com"
- "João";"joãozito@mail.com"
Add Comment
Please, Sign In to add comment