. * * @category PHP * @author V.Krishn * @copyright Copyright (c) 2012-2013 V.Krishn * @license GPL * @link http://github.com/insteps/phputils * @version 0.1.0 * */ /** * Parse a CSV string into an array for php 4+. * @param string $input String * @param string $delimiter String * @param string $enclosure String * @return array */ function str_getcsv4($input, $delimiter = ',', $enclosure = '"') { if( ! preg_match("/[$enclosure]/", $input) ) { return (array)preg_replace(array("/^\\s*/", "/\\s*$/"), '', explode($delimiter, $input)); } $token = "##"; $token2 = "::"; //alternate tokens "\034\034", "\035\035", "%%"; $t1 = preg_replace(array("/\\\[$enclosure]/", "/$enclosure{2}/", "/[$enclosure]\\s*[$delimiter]\\s*[$enclosure]\\s*/", "/\\s*[$enclosure]\\s*/"), array($token2, $token2, $token, $token), trim(trim(trim($input), $enclosure))); $a = explode($token, $t1); foreach($a as $k=>$v) { if ( preg_match("/^{$delimiter}/", $v) || preg_match("/{$delimiter}$/", $v) ) { $a[$k] = trim($v, $delimiter); $a[$k] = preg_replace("/$delimiter/", "$token", $a[$k]); } } $a = explode($token, implode($token, $a)); return (array)preg_replace(array("/^\\s/", "/\\s$/", "/$token2/"), array('', '', $enclosure), $a); } if ( ! function_exists('str_getcsv')) { function str_getcsv($input, $delimiter = ',', $enclosure = '"') { return str_getcsv4($input, $delimiter, $enclosure); } }