PHP fputcsv() Functie
❮ Referentie voor PHP-bestandssysteemVoorbeeld
Formatteer een regel als CSV en schrijft deze naar een open bestand:
<?php
$list = array
(
array("Peter", "Griffin" ,"Oslo", "Norway"),
array("Glenn", "Quagmire", "Oslo", "Norway")
);
$file = fopen("contacts.csv","w");
foreach ($list as $line) {
fputcsv($file, $line);
}
fclose($file);
?>
Definitie en gebruik
De functie fputcsv() maakt een regel op als CSV en schrijft deze naar een open bestand.
Tip: zie ook de functie fgetcsv() .
Syntaxis
fputcsv(file, fields, separator, enclosure,
escape)
Parameterwaarden
Parameter | Description |
---|---|
file | Required. Specifies the open file to write to |
fields | Required. Specifies which array to get the data from |
separator | Optional. A character that specifies the field separator. Default is comma ( , ) |
enclosure | Optional. A character that specifies the field enclosure character. Default is " |
escape | Optional. Specifies the escape character. Default is "\\". Can also be an empty string ("") which disables the escape mechanism |
Technische details
Winstwaarde: | De lengte van de geschreven string bij succes, ONWAAR bij mislukking |
---|---|
PHP-versie: | 5.1+ |
PHP-wijzigingslogboek: | PHP 7.4 - De escape - parameter accepteert nu een lege string om het escape-mechanisme uit te schakelen PHP 5.5 - De escape - parameter toegevoegd |
❮ Referentie voor PHP-bestandssysteem