Php str getcsv
From w3cyberlearnings
Contents |
PHP function str_getcsv
This function parses a CSV string into an array.
Syntax str_getcsv
- String: CSV String input
- delimiter: Set the delimiter (only single character is allow)
- enclosure: set the field enclosure (only single character is allow)
- escape: escapse character. Default- backslash (\)
str_getcsv (string, delimiter,enclosure, escape);
Relate function
If you want to split string into array by specifies a specific delimiter sees explode() function.
Example 1
<?php $input=<<<MYRECORD Name,Age,School,Score Yilin,21,MIT,80 Baobao,30,"Stanford University",90 Linlin,22,"A&M University of Texas",89 Mark Lee,32,"Grambling State University",88 MYRECORD; $record_csv = str_getcsv($input,"\n"); print_r($record_csv); ?>
Output
Array ( [0] => Name,Age,School,Score [1] => Yilin,21,MIT,80 [2] => Baobao,30,"Stanford University",90 [3] => Linlin,22,"A&M University of Texas",89 [4] => Mark Lee,32,"Grambling State University",88 )
Example 2
<?php $input=<<<MYRECORD Product: Price: ID:Location; Doggy Item:399 : 100B : Dallas, Tx; PHP prog:430: P340: Grambling, LA MYRECORD; $record_csv = str_getcsv($input,";"); print_r($record_csv); ?>
Output
Array ( [0] => Product: Price: ID:Location [1] => Doggy Item:399 : 100B : Dallas, Tx [2] => PHP prog:430: P340: Grambling, LA )
Example 3
<?php $input=<<<MYRECORD Product:Price:ID:Location: 'Doggy Item':399:100B:Dallas, Tx: 'PHP prog':430:P340:Grambling, LA MYRECORD; $record_csv = str_getcsv($input,":","'"); print_r($record_csv); ?>
Output
Array ( [0] => Product [1] => Price [2] => ID [3] => Location [4] => Doggy Item [5] => 399 [6] => 100B [7] => Dallas, Tx [8] => PHP prog [9] => 430 [10] => P340 [11] => Grambling, LA )