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.”

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

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

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

foreach ($lines as $line) {

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

print ("
") . $out . "
Written by doutdex in: PHP | Tags: ,

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a Reply

%d bloggers like this: