PHP Tutorial - PHP str_getcsv() Function






Definition

The str_getcsv() function parses a string for fields in CSV format and returns an array containing the fields read.

Syntax

PHP str_getcsv() Function has the following syntax.

str_getcsv(string,separator,enclosure,escape)

Parameter

Parameter Is Required Description
string Required.String value to parse
separator Optional.Field separator. Default is comma ( , )
enclosure Optional.Field enclosure character. Default is "
escape Optional. Escape character. Default is backslash (\)




Return

PHP str_getcsv() Function returns the CSV fields in an array.

Example

Get string value from a CSV line


<?php//from   w w w.  j a va2 s . c o m

$line = 'Demo "java2s.com." abc.jpg';

$parsed = str_getcsv(
    $line, # Input line
    ' ',   # Delimiter
    '"',   # Enclosure
    '\\'   # Escape char
);

var_dump( $parsed );

?>

The code above generates the following result.