Core Function CSV

From Sputnik Wiki
Revision as of 23:42, 1 December 2011 by UberFoX (Talk | contribs)
Jump to: navigation, search
CSV( <string>, <delim>, <quote>, <comment> )

Contents

Description

Split strings and parse a CSV text into an array.

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 "

comment

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

Default is "

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 0.

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.

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");
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox