PHP/File Directory/CSV File

Материал из Web эксперт
Версия от 07:04, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

fgetcsv.php

 
<?php
   $fh = fopen("s.csv", "r");
   while (list($name, $email, $phone) = fgetcsv($fh, 1024, ",")) {
      echo "<p>$name ($email) Tel. $phone</p>";
   }
?>



Generating comma-separated data

 
<?php
$sales = array( array("Northeast",12.54),
                array("All Regions",1597.34) );
$fh = fopen("sales.csv","w") or die("Can"t open sales.csv");
foreach ($sales as $sales_line) {
    if (fputcsv($fh, $sales_line) === false) {
        die("Can"t write CSV line");
    }
}
fclose($fh) or die("Can"t close sales.csv");
?>



Making a CSV-formatted string

 
function make_csv_line($values) {
    foreach($values as $i => $value) {
        if ((strpos($value, ",")  !== false) ||
            (strpos($value, """)  !== false) ||
            (strpos($value, " ")  !== false) ||
            (strpos($value, "\t") !== false) ||
            (strpos($value, "\n") !== false) ||
            (strpos($value, "\r") !== false)) {
            $values[$i] = """ . str_replace(""", """", $value) . """;
        }
    }
    return implode(",", $values) . "\n";
}



Parsing CSV Strings

 
<?php
function parse_csv($string, $delim = ",", $quote=""") {
    $new = str_replace("{$quote}{$quote}", $quote, $string);
    $matches = array();
    preg_match_all("/\s*({$quote}?)(.*?)\\1\s*(?:{$delim}|$)/",
        $new, $matches);
    array_pop($matches[2]);
    return $matches[2];
}
$str = "c "s""v""","s", "f\ny" ";
$values = parse_csv($str);
echo "<pre>";
print_r($values);
echo "</pre>";
?>



Printing comma-separated data

 
<?php
$sales = array( array("Northeast",12.54),
                array("All Regions",12.34) );
$fh = fopen("php://output","w");
foreach ($sales as $sales_line) {
    if (fputcsv($fh, $sales_line) === false) {
        die("Can"t write CSV line");
    }
}
fclose($fh);
?>



Putting comma-separated data into a string

 
<?php
$sales = array( array("Northeast",12.54),
                array("All Regions",1597.34) );
ob_start();
$fh = fopen("php://output","w") or die("Can"t open php://output");
foreach ($sales as $sales_line) {
    if (fputcsv($fh, $sales_line) ===  false) {
        die("Can"t write CSV line");
    }
}
fclose($fh) or die("Can"t close php://output");
$output = ob_get_contents();
ob_end_clean();
?>



Read csv file

 
<?php
$fh = fopen("csvdata.csv", "rb");
if (! $fh) {
    die("Can"t open csvdata.csv: $php_errormsg");
}
print "<table>\n";
       
for ($line = fgetcsv($fh, 1024); ! feof($fh); $line = fgetcsv($fh, 1024)) {
    print "<tr><td>" . implode("</td><td>", $line) . "</td></tr>\n";
}
print "</table>";
?>



Reading CSV data from a file

 
<?php
$fp = fopen("data.csv","r") or die("can"t open file");
print "<table>\n";
while($csv_line = fgetcsv($fp)) {
    print "<tr>";
    for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
        print "<td>".htmlentities($csv_line[$i])."</td>";
    }
    print "</tr>\n";
}
print "</table>\n";
fclose($fp) or die("can"t close file");
?>



Tell the web client to view the CSV file in a seprate program

 
header("Content-Disposition: attachment; filename="dishes.csv"");