aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV.Krishn <vkrishn4@gmail.com>2014-01-10 20:05:23 +0530
committerV.Krishn <vkrishn4@gmail.com>2014-01-10 20:05:23 +0530
commita22775bd1a043938f1b7ee8d13ad2feecc2a4693 (patch)
tree4abae4d249c0709248ebe5be974bfcdd15fd3f37
parentfbc771e5224893b5d9b054737c17fc4cce0f4ab0 (diff)
downloadphputils-a22775bd1a043938f1b7ee8d13ad2feecc2a4693.tar.bz2
Added function `str_getcsv4a` (different approach)v0.1.1
- Note: This function trims all values unlike str_getcsv (v5.3). - Note: This is alternate method for str_getcsv4.
-rw-r--r--docs/release-note/0.1.0.md6
-rw-r--r--docs/release-note/0.1.1.md5
-rw-r--r--str_getcsv4.php43
3 files changed, 48 insertions, 6 deletions
diff --git a/docs/release-note/0.1.0.md b/docs/release-note/0.1.0.md
index 69eb177..fd8d861 100644
--- a/docs/release-note/0.1.0.md
+++ b/docs/release-note/0.1.0.md
@@ -1,4 +1,4 @@
## PhpUtils
-Release date: 2012-03-06
-* Added functions str_getcsv4 and str_getcsv(wrapper for php 4+)
-** Note: This function trims all values unlike str_getcsv (v5.3).
+Release date: 2013-03-06
+* Added functions `str_getcsv4` and `str_getcsv`(wrapper for php 4+)
+** Note: This function trims all values unlike `str_getcsv` (v5.3).
diff --git a/docs/release-note/0.1.1.md b/docs/release-note/0.1.1.md
new file mode 100644
index 0000000..e9bb524
--- /dev/null
+++ b/docs/release-note/0.1.1.md
@@ -0,0 +1,5 @@
+## PhpUtils
+Release date: 2014-01-10
+* Added functions `str_getcsv4a` (different approach).
+** Note: This function trims all values unlike `str_getcsv` (v5.3).
+** Note: This is alternate method for `str_getcsv4`.
diff --git a/str_getcsv4.php b/str_getcsv4.php
index 90d9cdf..e2d6484 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-2013 V.Krishn <vkrishn4@gmail.com>
+ * @copyright Copyright (c) 2012-2014 V.Krishn <vkrishn4@gmail.com>
* @license GPL
* @link http://github.com/insteps/phputils
- * @version 0.1.0
+ * @version 0.1.1
*
*/
@@ -52,9 +52,46 @@
}
+/**
+ * Parse a CSV string into an array for php 4+. (Alternate)
+ * @param string $input String
+ * @param string $delimiter String
+ * @param string $enclosure String
+ * @return array
+ */
+ function str_getcsv4a($input, $delimiter = ',', $enclosure = '"') {
+
+ if( ! preg_match("/[$enclosure]/", $input) ) {
+ return (array)preg_replace(array("/^\\s*/", "/\\s*$/"), '', explode($delimiter, $input));
+ }
+
+ $a = explode($delimiter, $input);
+
+ foreach($a as $k=>$v) {
+
+ if ( preg_match("/{$enclosure}$/", rtrim($v)) ) {
+ $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);
+ }
+ if ( $on ) { $c[] = $v; continue; }
+
+ $b[] = trim($a[$k]);
+
+ }
+
+ return (array)$b;
+
+ }
+
if ( ! function_exists('str_getcsv')) {
function str_getcsv($input, $delimiter = ',', $enclosure = '"') {
- return str_getcsv4($input, $delimiter, $enclosure);
+ //return str_getcsv4($input, $delimiter, $enclosure); //OLDER
+ return str_getcsv4a($input, $delimiter, $enclosure); //NEWER
}
}