File Manipulation Code
  Home arrow File Manipulation Code arrow Bs_CsvUtil
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FILE MANIPULATION CODE

Bs_CsvUtil
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-11-27

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    class to read and write comma-separated-value files.

    url with examples: http://www.blueshoes.org/en/framework/util/csv_util/
    download: http://www.blueshoes.org/download/Bs_CsvUtil.class.php
    phpdoc: http://developer.blueshoes.org/phpdoc/Bs_CsvUtil.html

    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.
    this class is part of the blueshoes php application framework, see blueshoes.org.


    By : blueshoes

    <?php
    define('BS_CSVUTIL_VERSION', '4.0.$x$');


    /**
    * 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
    *
    * dependencies: none.
    *
    * @author andrej arn <andrej at blueshoes dot org>
    * @copyright blueshoes.org
    * @version 4.2.$id$
    * @package util
    * @access pseudostatic
    */
    class Bs_CsvUtil {


    /**
    * Constructor.
    */
    function Bs_CsvUtil() {
    }


    /**
    * reads in a cvs-file and returns it as a 2-dim vector.
    * @param string $fullPath (fullpath to the cvs file)
    * @param bool $checkMultiline (default is FALSE, see _checkMultiline())
    * @see csvArrayToArray()
    */
    function csvFileToArray($fullPath, $separator=';', $trim='none', $removeHeader=FALSE, $removeEmptyLines=FALSE, $checkMultiline=FALSE) {
    $fileContent = @file($fullPath);
    if (!$fileContent) return FALSE;

    //hrm, having similar prob as in csvStringToArray() here except this time i need it for \n not \r.
    //so let's remove that aswell ... --andrej
    while (list($k) = each($fileContent)) {
    if ((substr($fileContent[$k], -1) == "\r") || (substr($fileContent[$k], -1) == "\n")) {
    $fileContent[$k] = substr($fileContent[$k], 0, -1);
    }
    }
    reset($fileContent);

    if ($checkMultiline) $fileContent = $this->_checkMultiline($fileContent);
    return $this->csvArrayToArray($fileContent, $separator, $trim, $removeHeader, $removeEmptyLines);
    }


    /**
    * takes a csv-string and returns it as a 2-dim vector.
    * @param string $string
    * @param bool $checkMultiline (default is FALSE, see _checkMultiline())
    * @see csvArrayToArray()
    */
    function csvStringToArray($string, $separator=';', $trim='none', $removeHeader=FALSE, $removeEmptyLines=FALSE, $checkMultiline=FALSE) {
    if (empty($string)) return array();
    $array = explode("\n", $string);

    //short hack: on windows we should explode by "\r\n". if not, the elements in $array still end with \r.
    //so let's remove that ... --andrej
    while (list($k) = each($array)) {
    if (substr($array[$k], -1) == "\r") {
    $array[$k] = substr($array[$k], 0, -1);
    }
    }
    reset($array);

    if ((!is_array($array)) || empty($array)) return array();
    if ($checkMultiline) $array = $this->_checkMultiline($array);
    return $this->csvArrayToArray($array, $separator, $trim, $removeHeader, $removeEmptyLines);
    }


    /**
    *
    * 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.
    *
    * @access public pseudostatic
    * @param string $fullPath (fullpath to the cvs file)
    * @param array $array (hash or vector where the values are the csv lines)
    * @param string $separator (cell separator, default is ';')
    * @param 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.)
    * @param bool $removeHeader (default is FALSE. would remove the first line which usually is the title line.)
    * @param bool $removeEmptyLines (default is FALSE. would remove empty lines, that is, lines where the cells are empty. white spaces count as empty aswell.)
    * @return array (2-dim vector. it may be an empty array if there is no data.)
    * @throws bool FALSE on any error.
    * @see csvStringToArray()
    */
    function csvArrayToArray($array, $separator=';', $trim='none', $removeHeader=FALSE, $removeEmptyLines=FALSE) {
    switch ($trim) {
    case 'none':
    $trimFunction = FALSE;
    break;
    case 'left':
    $trimFunction = 'ltrim';
    break;
    case 'right':
    $trimFunction = 'rtrim';
    break;
    default: //'both':
    $trimFunction = 'trim';
    break;
    }

    $sepLength = strlen($separator);

    if ($removeHeader) {
    array_shift($array);
    }

    $ret = array();
    reset($array);
    while (list(,$line) = each($array)) {
    $offset = 0;
    $lastPos = 0;
    $lineArray = array();
    do {
    //find the next separator
    $pos = strpos($line, $separator, $offset);
    if ($pos === FALSE) {
    //no more separators.
    $lineArray[] = substr($line, $lastPos);
    break;
    }
    //now let's see if it is inside a field value (text) or it is a real separator.
    //it can only be a separator if the number of quotes (") since the last separator
    //is straight (not odd).
    $currentSnippet = substr($line, $lastPos, $pos-$lastPos);
    $numQuotes = substr_count($currentSnippet, '"');
    if ($numQuotes % 2 == 0) {
    //that's good, we got the next field. the separator was a real one.
    $lineArray[] = substr($line, $lastPos, $pos-$lastPos);
    $lastPos = $pos + $sepLength;
    } else {
    //have to go on, separator was inside a field value.
    }
    $offset = $pos + $sepLength;
    } while (TRUE);

    //trim if needed
    if ($trimFunction !== FALSE) {
    while (list($k) = each($lineArray)) {
    $lineArray[$k] = $trimFunction($lineArray[$k]);
    }
    reset($lineArray);
    }

    //remove quotes around cell values, and unescape other quotes.
    while (list($k) = each($lineArray)) {
    if ((substr($lineArray[$k], 0, 1) == '"') && (substr($lineArray[$k], 1, 1) != '"') && (substr($lineArray[$k], -1) == '"')) {
    //string has to look like "hello world" and may not look like ""hello.
    //if two quotes are together, it's an escaped one. csv uses ms-escape style.
    $lineArray[$k] = substr($lineArray[$k], 1, -1);
    }
    //now un-escape the other quotes
    $lineArray[$k] = str_replace('""', '"', $lineArray[$k]);
    }
    reset($lineArray);

    //removeEmptyLines
    $addIt = TRUE;
    if ($removeEmptyLines) {
    do {
    while (list($k) = each($lineArray)) {
    if (!empty($lineArray[$k])) break 2;
    }
    $addIt = FALSE;
    } while (FALSE);
    reset($lineArray);
    }

    if ($addIt) {
    $ret[] = $lineArray;
    }
    }

    return $ret;
    }


    /**
    * 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"?
    *
    * @access public
    * @param array $array (see above)
    * @param string $separator (default is ';')
    * @param 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.)
    * @param bool $removeEmptyLines (default is TRUE. removes "lines" that have no value, would come out empty.)
    * @return string (empty string if there is nothing at all)
    */
    function arrayToCsvString($array, $separator=';', $trim='none', $removeEmptyLines=TRUE) {
    if (!is_array($array) || empty($array)) return '';

    switch ($trim) {
    case 'none':
    $trimFunction = FALSE;
    break;
    case 'left':
    $trimFunction = 'ltrim';
    break;
    case 'right':
    $trimFunction = 'rtrim';
    break;
    default: //'both':
    $trimFunction = 'trim';
    break;
    }

    $ret = array();
    reset($array);
    if (is_array(current($array))) {
    while (list(,$lineArr) = each($array)) {
    if (!is_array($lineArr)) {
    //could issue a warning ...
    $ret[] = array();
    } else {
    $subArr = array();
    while (list(,$val) = each($lineArr)) {
    $val = $this->_valToCsvHelper($val, $separator, $trimFunction);
    $subArr[] = $val;
    }
    }
    $ret[] = join($separator, $subArr);
    }
    return join("\n", $ret);
    } else {
    while (list(,$val) = each($array)) {
    $val = $this->_valToCsvHelper($val, $separator, $trimFunction);
    $ret[] = $val;
    }
    return join($separator, $ret);
    }
    }


    /**
    * works on a string to include in a csv string/file.
    * @access private
    * @param string $val
    * @param string $separator
    * @param mixed $trimFunction (bool FALSE or 'rtrim' or so.)
    * @return string
    * @see arrayToCsvString() and others.
    */
    function _valToCsvHelper($val, $separator, $trimFunction) {
    if ($trimFunction) $val = $trimFunction($val);
    //if there is a separator (;) or a quote (") or a linebreak in the string, we need to quote it.
    $needQuote = FALSE;
    do {
    if (strpos($val, '"') !== FALSE) {
    $val = str_replace('"', '""', $val);
    $needQuote = TRUE;
    break;
    }
    if (strpos($val, $separator) !== FALSE) {
    $needQuote = TRUE;
    break;
    }
    if ((strpos($val, "\n") !== FALSE) || (strpos($val, "\r") !== FALSE)) { // \r is for mac
    $needQuote = TRUE;
    break;
    }
    } while (FALSE);
    if ($needQuote) {
    $val = '"' . $val . '"';
    }
    return $val;
    }



    /**
    * takes an array and combines elements (lines) if needed.
    * @access private
    * @param array $in
    * @return array
    */
    function _checkMultiline($in) {
    $ret = array();

    $stack = FALSE;
    reset($in);
    while (list(,$line) = each($in)) {
    $c = substr_count($line, '"');
    if ($c % 2 == 0) {
    if ($stack === FALSE) {
    $ret[] = $line;
    } else {
    $stack .= "\n" . $line;
    }
    } else {
    //odd number
    if ($stack === FALSE) {
    $stack = $line;
    } else {
    $ret[] = $stack . "\n" . $line;
    $stack = FALSE;
    }
    }
    }
    return $ret;
    }


    } // end Class



    //remove this (it shows the source code):
    if (basename($_SERVER['PHP_SELF']) == 'Bs_CsvUtil.class.php') {
    ###################################################################################################
    highlight_string(join('', file(__FILE__)));
    }

    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More File Manipulation Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    Check out the new Jazz space on developerWorks

    <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts.
    FREE! Go There Now!


    NEW! Driving Business Success with Rational Process Library

    Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance.
    FREE! Go There Now!


    NEW! Achieving True Agility -- How process can change the behavior of your tools

    Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools.
    FREE! Go There Now!


    NEW! Applying lean thinking to the governance of software development

    Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations.
    FREE! Go There Now!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered!
    FREE! Go There Now!


    NEW! Trial download: IBM Informix Dynamic Server Express Edition V11.0

    Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data.
    FREE! Go There Now!


    NEW! Try IBM Rational Asset Manager V7.0 online!

    You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online.
    FREE! Go There Now!


    NEW! Webcast: Extreme transaction processing with WebSphere Extended Deployment

    In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications.
    FREE! Go There Now!


    NEW! Webcast: Quickly provide customized, integrated user interfaces with Lotus Notes 8

    IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community.
    FREE! Go There Now!


    NEW! Webcast: WebSphere Process Server

    WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    FILE MANIPULATION CODE ARTICLES

    - Bandwidth Control with pure PHP
    - Eazy Gallery
    - file_get_contents for PHP < 4.3.0
    - PHP Class: Image Snapshot 1.3
    - Universal downloader
    - Image Gallery v2.0
    - Free/Used Disk Space
    - Directory Lister
    - Directory image view, with selective hidden
    - Move or Copy a Directory (and files and sub ...
    - Ensure_Sub_Directory_Exists
    - Wedit
    - Form Examples Text Boxes to Drop Downs
    - myFiles
    - List files in a directory, no subdirectories





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek