diff options
-rw-r--r-- | str_getcsv4.php | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/str_getcsv4.php b/str_getcsv4.php index 35e0962..0758666 100644 --- a/str_getcsv4.php +++ b/str_getcsv4.php @@ -15,10 +15,10 @@ * * @category PHP * @author V.Krishn <vkrishn4@gmail.com> - * @copyright Copyright (c) 2012-2015 V.Krishn <vkrishn4@gmail.com> + * @copyright Copyright (c) 2012-2024 V.Krishn <vkrishn4@gmail.com> * @license GPL * @link http://github.com/insteps/phputils - * @version 0.1.2 + * @version 0.1.3 * */ @@ -31,7 +31,7 @@ */ function str_getcsv4($input, $delimiter = ',', $enclosure = '"') { - if( ! preg_match("/[$enclosure]/", $input) ) { + if ( ! preg_match("/[$enclosure]/", $input) ) { return (array)preg_replace(array("/^\\s*/", "/\\s*$/"), '', explode($delimiter, $input)); } @@ -61,7 +61,7 @@ */ function str_getcsv4a($input, $delimiter = ',', $enclosure = '"') { - if( ! preg_match("/[$enclosure]/", $input) ) { + if ( ! preg_match("/[$enclosure]/", $input) ) { return (array)preg_replace(array("/^\\s*/", "/\\s*$/"), '', explode($delimiter, $input)); } @@ -71,13 +71,13 @@ foreach($a as $k=>$v) { if ( preg_match("/{$enclosure}$/", rtrim($v)) ) { - $on = 0; - $c[] = $v; $b[] = trim(trim(implode(',', $c)), $enclosure); - unset($c); continue; + $on = 0; + $c[] = $v; $b[] = trim(trim(implode(',', $c)), $enclosure); + unset($c); continue; } if ( preg_match("/^{$enclosure}/", ltrim($v)) ) { - $on = 1; unset($c); - $v = ltrim(ltrim($v), $enclosure); + $on = 1; unset($c); + $v = ltrim(ltrim($v), $enclosure); } if ( $on ) { $c[] = $v; continue; } @@ -89,6 +89,30 @@ } +/** + * Process an Array to CSV string. + * @param string $fields Array + * @param string $mode String + * @param string $size Integer + * @return string + */ +function csvstr(array $fields, $mode='+', int $size=1) : string { + + //$fp = fopen('php://memory', 'r'.$dbtable_mode); # ALT + $sizeMBs = $size * 1024 * 1024; + // output upto 1MB is kept in memory, + // if it becomes bigger it will be written to a temporary file + $fp = fopen("php://temp/maxmemory:$sizeMBs", 'r'.$mode); + if ( fputcsv($fp, $fields) === false ) { + return false; + } + rewind($fp); + $csv_line = stream_get_contents($fp); // string + fclose($fp); + return rtrim($csv_line); + +} + if ( ! function_exists('str_getcsv')) { function str_getcsv($input, $delimiter = ',', $enclosure = '"') { //return str_getcsv4($input, $delimiter, $enclosure); //OLDER |