May
15
2008

Función para Importar archivo (parser) a (from) CSV a variable PHP

Este código fue extraído de otro blog que ha sido probado y funciona correctamente.

“This is a snippet of code that I had posted on another of my websites and in doing some cleanup it really fits better here.

I spent last evening working on a Comma Separated Values (CSV) list of titles for a project I’m working on. Below is a snippet of code I worked with. The first method from the PHP Cookbook suggests using fgetcsv(). That woks pretty well but the native function in PHP doesn’t handle values (fields) that contain separators (commas) very well and doesn’t have the concept of multiple values being delimited by a different delimiter within a field. So I whipped up the parser that comes second in the example below.”


<?php
$fp = fopen('filename','r') or die("can't open file");
print ('
\n');
while($csv_line = fgetcsv($fp,1024, ",", "\"")) {
print '
');
for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
print '


';
}
print ("

\n");
}
print ("
'. str_replace (";\"", " ", $csv_line[$i]).'
\n");*/ fclose($fp) or die("can't close file"); $bas = file_get_contents ("filename"); print ("
\n");

$lines = explode("\n", $bas);

foreach ($lines as $line) {

print("
");
$line = ltrim($line, "\"");
$line = rtrim($line, "\"");
$line = rtrim($line);
$out = str_replace("\";\"", "
", $line);
$out = str_replace("\",\"", "
");
print ("
\n");
};
print ("
", $out);

print ("
") . $out . "
\n");
?>

Related posts:

  1. Función para listar directorios y archivos recursivamente PHP
  2. Upload file (cargar archivo) PHP
  3. Problema con Scrollkeeper ubuntu /var/lib/scrollkeeper/C/scrollkeeper_extended_cl.xml:995: parser error : Opening and ending tag mismatch: tocsect2 line 993 and tocsect3
  4. Comparación (Comparing) PHP and Perl
  5. Configuración de php.ini (seguro)

Written by doutdex in: PHP | Tags: ,

No Comments »

RSS feed for comments on this post. TrackBack URL


Leave a Reply

TheBuckmaker.com Wordpress Themes | Geld von Privat, Streaming Audio