Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 57
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
O wysokich kompetencjach zawodowych Darka nie ma co dyskutować. Wszyscy chyba jesteśmy zgodni co do tego, że Jego wiedza na polu informatycznym jest bogata i zasługuje na uznanie. Swego czasu zwróciłem się z prośbą o pomoc w realizacji małego projektu internetowego. Projekt był niewielki, jednak jego realizacja wymagała pewnego doświadczenia. Darek podjął się tego zlecenia, wykonał je szybko i sprawnie. Podczas realizacji służył doradztwem, jednak w żaden sposób nie narzucał swojego zdania. O prawidłowości Jego koncepcji przekonałem się dopiero po pewnym czasie. To, czego ja nie dostrzegałem, On dostrzegał i zwracał na to moją uwagę. Darek dał się poznać nie tylko, jako dobry fachowiec, co przede wszystkim okazał się być rzetelnym i uczciwym kontrahentem. Tak więc nie dość, że fachowiec, to jeszcze uczciwy. Polecam usługi Darka wszystkim tym, którzy szukają fachowej pomocy przy realizacji nawet najbardziej złożonych projektów.

Dariusz Żwan
Actuarius.pl

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

fputcsv

(PHP 5)

fputcsv --  Format line as CSV and write to file pointer

Description

int fputcsv ( resource handle [, array fields [, string delimiter [, string enclosure]]] )

fputcsv() formats a line (passed as a fields array) as CSV and write it to the specified file handle. Returns the length of the written string, or FALSE on failure.

The optional delimiter parameter sets the field delimiter (one character only). Defaults as a comma: ,.

The optional enclosure parameter sets the field enclosure (one character only) and defaults to a double quotation mark: ".

Przykład 1. fputcsv() example

<?php

$list
= array (
  
'aaa,bbb,ccc,dddd',
  
'123,456,789',
  
'"aaa","bbb"'
);

$fp = fopen('file.csv', 'w');

foreach (
$list as $line) {
  
fputcsv($fp, split(',', $line));
}

fclose($fp);
?>

Notatka: W przypadku problemów z rozpoznawaniem znaków końca linii przez PHP przy czytaniu plików stworzonych lub znajdujących się na komputerach Macintosh, może pomóc włączenie dyrektywy konfiguracji auto_detect_line_endings.

See also fgetcsv().




User Contributed Notes

heather at heathercash dot com
26-Sep-2005 01:18

Here is an adaptation to boonerunner's function for fputcsv.
It uses a 2-dimensional array.
Each sub-array is a line in the csv file which then ends up being seperated by commas.

function fputcsv($filePointer,$dataArray,$delimiter=",",$enclosure="\""){
   // Write a line to a file
   // $filePointer = the file resource to write to
   // $dataArray = the data to write out
   // $delimeter = the field separator
    
   // Build the string
   $string = "";
 
   // for each array element, which represents a line in the csv file...
   foreach($dataArray as $line){

       // No leading delimiter
       $writeDelimiter = FALSE;
      
       foreach($line as $dataElement){
           // Replaces a double quote with two double quotes
           $dataElement=str_replace("\"", "\"\"", $dataElement);
          
           // Adds a delimiter before each field (except the first)
           if($writeDelimiter) $string .= $delimiter;
          
           // Encloses each field with $enclosure and adds it to the string
           $string .= $enclosure . $dataElement . $enclosure;
          
           // Delimiters are used every time except the first.
           $writeDelimiter = TRUE;
       }
       // Append new line
       $string .= "\n";

   } // end foreach($dataArray as $line)

   // Write the string to the file
   fwrite($filePointer,$string);
}


boonerunner at hotmail dot com
16-Sep-2005 04:47

Here is an adaption of the above code that adds support for double quotes inside a field. (One double quote is replaced with a pair of double quotes per the CSV format).

<?php
 
function fputcsv($filePointer,$dataArray,$delimiter,$enclosure)
  {
 
// Write a line to a file
  // $filePointer = the file resource to write to
  // $dataArray = the data to write out
  // $delimeter = the field separator
 
  // Build the string
 
$string = "";
 
 
// No leading delimiter
 
$writeDelimiter = FALSE;
  foreach(
$dataArray as $dataElement)
   {
  
// Replaces a double quote with two double quotes
  
$dataElement=str_replace("\"", "\"\"", $dataElement);
  
  
// Adds a delimiter before each field (except the first)
  
if($writeDelimiter) $string .= $delimiter;
  
  
// Encloses each field with $enclosure and adds it to the string
  
$string .= $enclosure . $dataElement . $enclosure;
  
  
// Delimiters are used every time except the first.
  
$writeDelimiter = TRUE;
   }
// end foreach($dataArray as $dataElement)
 
  // Append new line
 
$string .= "\n";
 
 
// Write the string to the file
 
fwrite($filePointer,$string);
  }
?>


twebb at boisecenter dot com
21-Jan-2005 02:54

What about cells that span multiple lines?  This function allows for cells to contain newlines:

function fputcsv($handle, $row, $fd=',', $quot='"')
{
   $str='';
   foreach ($row as $cell)
   {
     $cell = str_replace($quot, $quot.$quot, $cell);
        
     if (strchr($cell, $fd) !== FALSE || strchr($cell, $quot) !== FALSE || strchr($cell, "\n") !== FALSE)
     {
         $str .= $quot.$cell.$quot.$fd;
     }
     else
     {
         $str .= $cell.$fd;
     }
   }

   fputs($handle, substr($str, 0, -1)."\n");

   return strlen($str);
}

I found this reference on the web:
http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm


drew at zitnay dot com
22-Nov-2004 06:42

I found the following problems with the below function:

- when calling str_replace(), you must assign $cell the return value or nothing gets saved

- when using strchr(), you should explicitly check !== FALSE, or it'll treat a return value of 0 (found the character at string position 0) as FALSE

- Excel seems to quote not only fields containing commas, but fields containing quotes as well, so I've added another strchr() for quotes; I'm not saying Microsoft knows the correct way for sure, but it seems reasonable to me

- the original function put a space after each comma; that might be legal, I don't know, but I've never seen it (and I don't think it is, because then how would you indicate you wanted a field to start with a space other than by quoting it?)

- the original function didn't correctly return the length of the data outputted

Here's the function, fixed up a bit:

function fputcsv($handle, $row, $fd=',', $quot='"')
{
   $str='';
   foreach ($row as $cell) {
       $cell=str_replace(Array($quot,        "\n"),
                         Array($quot.$quot,  ''),
                         $cell);
       if (strchr($cell, $fd)!==FALSE || strchr($cell, $quot)!==FALSE) {
           $str.=$quot.$cell.$quot.$fd;
       } else {
           $str.=$cell.$fd;
       }
   }

   fputs($handle, substr($str, 0, -1)."\n");

   return strlen($str);
}

Drew


arthur.at.korn.ch
19-Nov-2004 06:56

The function in the prior comment doesn't escape quotes in fields, here mine:

function fputcsv($handle, $row, $fd=',', $quot='"')
{
   $str='';
   foreach ($row as $cell) {
       str_replace(Array($quot,        "\n"),
                   Array($quot.$quot,  ''),
                   $cell);
       if (strchr($cell, $fd)) {
           $str.=$quot.$cell.$quot.$fd.' ';
       } else {
           $str.=$cell.$fd.' ';
       }
   }

   fputs($handle, substr($str, 0, -2)."\n");

   return $str-1;
}


arthur at mclean dot ws
11-Nov-2004 11:10

Here's a simplistic fputcsv function that you can use until the real one gets out of CVS:

function fputcsv($filePointer, $dataArray, $delimiter, $enclosure){
     // Write a line to a file
     // $filePointer = the file resource to write to
     // $dataArray = the data to write out
     // $delimeter = the field separator
    
     // Build the string
     $string = "";
     $writeDelimiter = FALSE;
     foreach($dataArray as $dataElement){
       if($writeDelimiter) $string .= $delimiter;
       $string .= $enclosure . $dataElement . $enclosure;
       $writeDelimiter = TRUE;
       } // end foreach($dataArray as $dataElement)
    
     // Append new line
     $string .= "\n";
    
     // Write the string to the file
     fwrite($filePointer, $string);
    
     } // end function fputcsv($filePointer, $dataArray, $delimiter)


 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt