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
Links
Filesystem
- basename
- chgrp
- chmod
- chown
- clearstatcache
- copy
- dirname
- disk_free_space
- disk_total_space
- diskfreespace
- fclose
- feof
- fflush
- fgetc
- fgetcsv
- fgets
- file
- file_exists
- file_get_contents
- file_put_contents
- fileatime
- filectime
- filegroup
- fileinode
- filemtime
- fileowner
- fileperms
- filesize
- filetype
- flock
- fnmatch
- fopen
- fpassthru
- fputs
- fread
- fscanf
- fseek
- fstat
- ftell
- ftruncate
- fwrite
- glob
- is_dir
- is_executable
- is_file
- is_link
- is_readable
- is_uploaded_file
- is_writable
- is_writeable
- lchgrp
- lchown
- link
- linkinfo
- lstat
- mkdir
- move_uploaded_file
- pathinfo
- pclose
- popen
- readfile
- readlink
- realpath
- realpath_cache_get
- realpath_cache_size
- rename
- rewind
- rmdir
- set_file_buffer
- stat
- symlink
- tempnam
- tmpfile
- touch
- umask
- unlink