Core Function CSV

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Remarks)
Line 1: Line 1:
 
<pre>
 
<pre>
CSV( <string>, <delim>, <quote>, <comment>, <trimtype> )
+
CSV( <string>, <delim>, <quote>, <quoteescape>, <comment>, <trimtype> )
 
</pre>
 
</pre>
  
Line 17: Line 17:
 
Optional; The Char to use as the Delimiter for seperation of the data.
 
Optional; The Char to use as the Delimiter for seperation of the data.
  
Default is ,
+
Default is ','
  
 
==== quote ====
 
==== quote ====
Line 23: Line 23:
 
Optional; The Char to use as the Quote this will be allowed to contain the Delimiter inside it.
 
Optional; The Char to use as the Quote this will be allowed to contain the Delimiter inside it.
  
Default is "
+
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 ====
 
==== comment ====
Line 29: Line 35:
 
Optional; The Char to use as the Comment and it will be removed from the text with no side effects.
 
Optional; The Char to use as the Comment and it will be removed from the text with no side effects.
  
Default is "
+
Default is '#'
 +
 
 +
Can be set to "\0" to disable need for comment
  
 
==== trimtype ====
 
==== trimtype ====
Line 87: Line 95:
 
}
 
}
 
println("ROW ENDS");
 
println("ROW ENDS");
 +
}
 +
</syntaxhighlight>
 +
 +
Example of having a Quote inside quotes using the QuiteEscape param
 +
<syntaxhighlight lang="sputnik">
 +
$a = "100, 200, '47, \\'77', 300, 400, 500";
 +
foreach(CSV($a, ",", "'", '\\')[0] as $item)
 +
{
 +
println("Item '$item'");
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 17:26, 23 August 2013

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

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 '"'

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