Core Function CSV

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
Line 6: Line 6:
  
 
Split strings and parse a CSV text into an array.
 
Split strings and parse a CSV text into an array.
 +
 +
This function is useful for reading columns of data from a text file where the columns (fields) are delimited with a specific character or characters.
  
 
=== Parameters ===
 
=== Parameters ===

Revision as of 17:31, 23 August 2013

CSV( <string>, <delim>, <quote>, <quoteescape>, <comment>, <trimtype> )

Contents

Description

Split strings and parse a CSV text into an array.

This function is useful for reading columns of data from a text file where the columns (fields) are delimited with a specific character or characters.

Parameters

string

The string to parse.

delim

Optional; The Char to use as the Delimiter for seperation of the data.

Default is ','

quote

Optional; The Char to use as the Quote this will be allowed to contain the Delimiter inside it.

Default is '"'

quoteescape

Optional; The Char to use for escaping the Quote this will be used to allow a Quote to appear inside quotes.

Default is "\\"

comment

Optional; The Char to use as the Comment and it will be removed from the text with no side effects.

Default is '#'

Can be set to "\0" to disable need for comment

trimtype

Optional; Determines how values should be trimmed.

0 = Trim Unquoted Only (Default) 1 = Trim Quoted Only 2 = Trim All 3 = Trim None

Default is: 0

Return Value

Success: Returns a 2 dimensional array where the first dimension is the LINES or ROWS and the second dimension is the parsed data in an array.

Failure: Returns empty array.

Remarks

CSV Parsing is better than simply using Split() on complex data since Split() does not think about finding the split char inside "" quotes etc where as this CSV function does.

This CSV parser supports fields spanning multiple lines. The only restriction is that they must be quoted, otherwise it would not be possible to distinguish between malformed data and multi-line values.

Example

A simple split a string using , for the Delimiter

$a = "100, 200, 300";
foreach(CSV($a)[0] as $item)
{
	println("Item '$item'");
}

A more complex split where the , Delimiter is also contained within ' string '

$a = "100, 200, '47, 77', 300, 400, 500";
foreach(CSV($a, ",", "'")[0] as $item)
{
	println("Item '$item'");
}

A larger example involving multiple lines of text to parse and the , Delimiter is also contained within ' string '

$a =  "100, 200, '47, 77', 300, 400, 500\n";
$a .= "122.33 'hello, yay', 55, 77\n";
$a .= "a, b, c, d";
foreach(CSV($a, ",", "'") as $lines)
{
	println("ROW BEGINS");
	foreach($lines as $item)
	{
		println("Item '$item'");
	}
	println("ROW ENDS");
}

Example of having a Quote inside quotes using the QuiteEscape param

$a = "100, 200, '47, \\'77', 300, 400, 500";
foreach(CSV($a, ",", "'", '\\')[0] as $item)
{
	println("Item '$item'");
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox