blueshoes php application framework and cms            core_util
[ class tree: core_util ] [ index: core_util ] [ all elements ]

Class: Bs_CsvUtil

Source Location: /core/util/Bs_CsvUtil.class.php

Class Overview

Bs_Object
   |
   --Bs_CsvUtil

csv util class. csv = comma separated value.


Author(s):

Version:

  • 4.3.$Revision: 1.2 $ $Date: 2003/10/29 17:48:43 $

Copyright:

  • blueshoes.org

Methods


Inherited Variables

Inherited Methods

Class: Bs_Object

Bs_Object::Bs_Object()
Bs_Object::getErrors()
Basic error handling: Get *all* errors as string array from the global Bs_Error-error stack.
Bs_Object::getLastError()
Basic error handling: Get last error string from the global Bs_Error-error stack.
Bs_Object::getLastErrors()
Basic error handling: Get last errors string array from the global Bs_Error-error stack sinc last call of getLastErrors().
Bs_Object::persist()
Persists this object by serializing it and saving it to a file with unique name.
Bs_Object::setError()
Basic error handling: Push an error string on the global Bs_Error-error stack.
Bs_Object::toHtml()
Dumps the content of this object to a string using PHP's var_dump().
Bs_Object::toString()
Dumps the content of this object to a string using PHP's var_dump().
Bs_Object::unpersist()
Fetches an object that was persisted with persist()

Class Details

[line 38]
csv util class. csv = comma separated value.

features:

  • supports any separator char sequence, default is semicolon ";"
  • supports separator characters in the values. eg you use a ; as separator, your line may look like blah;hello world;"foo";"foo;bar";"this is a ""string""";got it?;foo as you can see, the values can be in "quotes". if your text uses quotes itself as in the "string" example, they are escaped in ms-style with 2 quotes. and by using quotes we can even have your separator inside the text (example "foo;bar").
  • line breaks. a csv line may spread over multiple lines using crlf in a field value. see the checkMultiline param and the _checkMultiline() method.
missing:
  • option to change quote char (") to something else
thanks to: steffen at hung dot ch

example: $c =& new Bs_CsvUtil; dump($c->csvFileToArray('/some/path/file.csv'));

dependencies: none.




Tags:

version:  4.3.$Revision: 1.2 $ $Date: 2003/10/29 17:48:43 $
copyright:  blueshoes.org
author:  andrej arn <at blueshoes dot org>


[ Top ]


Class Methods


constructor Bs_CsvUtil [line 44]

Bs_CsvUtil Bs_CsvUtil( )

Constructor.



[ Top ]

method arrayToCsvString [line 264]

string arrayToCsvString( array $array, [string $separator = ';'], [string $trim = 'none'], [bool $removeEmptyLines = TRUE])

takes an array and creates a csv string from it.

the given param $array may be a simple 1-dim array like this: $arr = array('madonna', 'alanis morisette', 'falco'); that will result in the string: "madonna;alanis morisette;falco"

if the param is a 2-dim array, it goes like this: $arr = array( array('madonna', 'pop', 'usa'), array('alanis morisette', 'rock', 'canada'), array('falco', 'pop', 'austria'), ); result: madonna;pop;usa alanis morisette;rock;canada falco;pop;austria

todo: add param "fill to fit max length"?




Tags:

return:  (empty string if there is nothing at all)
access:  public


Parameters:

array   $array   (see above)
string   $separator   (default is ';')
string   $trim   (if we should trim the cells, default is 'none', can also be 'left', 'right' or 'both'. 'none' kinda makes it faster, omits many function calls, remember that.)
bool   $removeEmptyLines   (default is TRUE. removes "lines" that have no value, would come out empty.)

[ Top ]

method csvArrayToArray [line 145]

array csvArrayToArray( array $array, [string $separator = ';'], [string $trim = 'none'], [bool $removeHeader = FALSE], [bool $removeEmptyLines = FALSE], string $fullPath)

reads in a cvs array and returns it as a 2-dim vector.

cvs = comma separated value. you can easily export that from an excel file for example. it looks like:

headerCellOne;headerCellTwo;headerCellThree dataCellOne;dataCellTwo;dataCellThree apple;peach;banana;grapefruit linux;windows;mac 1;2;3

note I: all returned array elements are strings even if the values were numeric. note II: it may be that one array has another array-length than another. in the example above, the fruits have 4 elements while the others just have 3. this is not catched. ideally every sub-array would have 4 elements. this would have to be added when needed, maybe with another param in the function call.




Tags:

return:  (2-dim vector. it may be an empty array if there is no data.)
see:  Bs_CsvUtil::csvStringToArray()
throws:  bool FALSE on any error.


Parameters:

string   $fullPath   (fullpath to the cvs file)
array   $array   (hash or vector where the values are the csv lines)
string   $separator   (cell separator, default is ';')
string   $trim   (if we should trim the cells, default is 'none', can also be 'left', 'right' or 'both'. 'none' kinda makes it faster, omits many function calls, remember that.)
bool   $removeHeader   (default is FALSE. would remove the first line which usually is the title line.)
bool   $removeEmptyLines   (default is FALSE. would remove empty lines, that is, lines where the cells are empty. white spaces count as empty aswell.)

[ Top ]

method csvFileToArray [line 59]

array csvFileToArray( string $fullPath, [string $separator = ';'], [mixed $trim = 'none'], [bool $removeHeader = FALSE], [bool $removeEmptyLines = FALSE], [bool $checkMultiline = FALSE])

reads in a cvs-file and returns it as a 2-dim vector.



Tags:



Parameters:

string   $fullPath   (fullpath to the cvs file)
string   $separator   (default is ';'. set to NULL if you don't know, it will then be guessed. see $this->guessSeparator().)
bool   $removeHeader   (default is FALSE, if the first line should be removed cause it's a header.)
bool   $removeEmptyLines   (if empty lines should be removed. default is FALSE... you might want to change this. especially empty lines at the eof are annoying.)
bool   $checkMultiline   (default is FALSE, see _checkMultiline())

[ Top ]

method csvStringToArray [line 93]

array csvStringToArray( string $string, [string $separator = ';'], [mixed $trim = 'none'], [bool $removeHeader = FALSE], [bool $removeEmptyLines = FALSE], [bool $checkMultiline = FALSE])

takes a csv-string and returns it as a 2-dim vector.



Tags:



Parameters:

string   $string  
string   $separator   (default is ';'. set to NULL if you don't know, it will then be guessed. see $this->guessSeparator().)
bool   $removeHeader   (default is FALSE, if the first line should be removed cause it's a header.)
bool   $removeEmptyLines   (if empty lines should be removed. default is FALSE... you might want to change this. especially empty lines at the eof are annoying.)
bool   $checkMultiline   (default is FALSE, see _checkMultiline())

[ Top ]

method guessSeparator [line 326]

string guessSeparator( array $cvsArray)

tries to find out the separator used to separate the cvs values.

the order of guessing is: ';', ',', "\t", '|', ' '

note: you need to clean out multilines before you try this method. see $this->_checkMultiline() (maybe make it public?)

remember, it's GUESSseparator, there are situations that cause problems. i recommend that you use ';' if it cannot be detected as it's the de-facto standard.




Tags:

return:  (separator char sequence, eg ';')
since:  bs4.4
throws:  bool FALSE (if no luck, or no data.)
access:  public


Parameters:

array   $cvsArray  

[ Top ]


Documentation generated on Mon, 29 Dec 2003 21:08:20 +0100 by phpDocumentor 1.2.3