Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Jonas Raoni Soares Silva http://jsfromhell.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

CSV Parser / Writer for PHP

CSV Parser / Writer

Example A:

   1  
   2  //cell separator, row separator, value enclosure
   3  $csv = new CSV(';', "\r\n", '"');
   4  
   5  //parse the string content
   6  $csv->setContent(file_get_contents('data.csv'));
   7  
   8  //returns an array with the CSV data
   9  print_r($csv->getArray());
  10  


Exemple B:

   1  
   2  $csv = new CSV(';', "\r\n", '"');
   3  //sets up the content through an array
   4  $csv->setArray(
   5  	array(
   6  		array('col"una1', "colu\r\nna2"),
   7  		array('col;una3', 'coluna4')
   8  	)
   9  );
  10  //retorns string with the CSV representation
  11  print $csv->getContent();
  12  


   1  
   2  <?php
   3  //+ Jonas Raoni Soares Silva
   4  //@ http://jsfromhell.com
   5  class CSV{
   6  	var $cellDelimiter;
   7  	var $valueEnclosure;
   8  	var $rowDelimiter;
   9  
  10  	function CSV($cellDelimiter, $rowDelimiter, $valueEnclosure){
  11  		$this->cellDelimiter = $cellDelimiter;
  12  		$this->valueEnclosure = $valueEnclosure;
  13  		$this->rowDelimiter = $rowDelimiter;
  14  		$this->o = array();
  15  	}
  16  	function getArray(){
  17  		return $this->o;
  18  	}
  19  	function setArray($o){
  20  		$this->o = $o;
  21  	}
  22  	function getContent(){
  23  		if(!(($bl = strlen($b = $this->rowDelimiter)) && ($dl = strlen($d = $this->cellDelimiter)) && ($ql = strlen($q = $this->valueEnclosure))))
  24  			return '';
  25  		for($o = $this->o, $i = -1; ++$i < count($o);){
  26  			for($e = 0, $j = -1; ++$j < count($o[$i]);)
  27  				(($e = strpos($o[$i][$j], $q) !== false) || strpos($o[$i][$j], $b) !== false || strpos($o[$i][$j], $d) !== false)
  28  				&& $o[$i][$j] = $q . ($e ? str_replace($q, $q . $q, $o[$i][$j]) : $o[$i][$j]) . $q;
  29  			$o[$i] = implode($d, $o[$i]);
  30  		}
  31  		return implode($b, $o);
  32  	}
  33  	function setContent($s){
  34  		$this->o = array();
  35  		if(!strlen($s))
  36  			return true;
  37  		if(!(($bl = strlen($b = $this->rowDelimiter)) && ($dl = strlen($d = $this->cellDelimiter)) && ($ql = strlen($q = $this->valueEnclosure))))
  38  			return false;
  39  		for($o = array(array('')), $this->o = &$o, $e = $r = $c = 0, $i = -1, $l = strlen($s); ++$i < $l;){
  40  			if(!$e && substr($s, $i, $bl) == $b){
  41  				$o[++$r][$c = 0] = '';
  42  				$i += $bl - 1;
  43  			}
  44  			elseif(substr($s, $i, $ql) == $q){
  45  				$e ? (substr($s, $i + $ql, $ql) == $q ?
  46  				$o[$r][$c] .= substr($s, $i += $ql, $ql) : $e = 0)
  47  				: (strlen($o[$r][$c]) == 0 ? $e = 1 : $o[$r][$c] .= substr($s, $i, $ql));
  48  				$i += $ql - 1;
  49  			}
  50  			elseif(!$e && substr($s, $i, $dl) == $d){
  51  				$o[$r][++$c] = '';
  52  				$i += $dl - 1;
  53  			}
  54  			else
  55  				$o[$r][$c] .= $s[$i];
  56  		}
  57  		return true;
  58  	}
  59  }
  60  ?>
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS