HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

fputcsv

Description

The fputcsv of Filesystem for PHP format line as CSV and write to file pointer.

Syntax

fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\\" ]]] ) : int

Parameters

handle

The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).

fields

An array of strings.

delimiter

The optional delimiter parameter sets the field delimiter (one character only).

enclosure

The optional enclosure parameter sets the field enclosure (one character only).

escape_char

The optional escape_char parameter sets the escape character (at most one character). An empty string ("") disables the proprietary escape mechanism.

Note: If an enclosure character is contained in a field, it will be escaped by doubling it, unless it is immediately preceded by an escape_char.

Return

Returns the length of the written string or FALSE on failure.

Examples

1 · handle fields

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/csv/file.csv";
$mode = "w+";

$handle = fopen($filename, $mode);

    $fields = array("H", "e", "l", "l", "o");

    $return = fputcsv($handle, $fields);

    rewind($handle);
    fpassthru($handle);

    echo $return;

fclose($handle);
H,e,l,l,o
10

2 · delimiter

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/csv/file.csv";
$mode = "w+";

$handle = fopen($filename, $mode);

    $fields = array("H", "e", "l", "l", "o");
    $delimiter = " ";

    $return = fputcsv($handle, $fields, $delimiter);

    rewind($handle);
    fpassthru($handle);

    echo $return;

fclose($handle);
H e l l o
10

3 · enclosure

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/csv/file.csv";
$mode = "w+";

$handle = fopen($filename, $mode);

    $fields = array("H", "e", "'l'", "'l'", "o");
    $delimiter = " ";
    $enclosure = "'";

    $return = fputcsv($handle, $fields, $delimiter, $enclosure);

    rewind($handle);
    fpassthru($handle);

    echo $return;

fclose($handle);
H e '''l''' '''l''' o
22

4 · escape_char

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/csv/file.csv";
$mode = "w+";

$handle = fopen($filename, $mode);

    $fields = array("H", "e", "'l'", "'l'", "o");
    $delimiter = " ";
    $enclosure = "'";
    $escape_char = "'";

    $return = fputcsv($handle, $fields, $delimiter, $enclosure, $escape_char);

    rewind($handle);
    fpassthru($handle);

    echo $return;

fclose($handle);
H e ''l'' ''l'' o
18