note 41426 added to function.split

From: Date: Fri, 09 Apr 2004 15:54:14 +0000
Subject: note 41426 added to function.split
Groups: php.notes 
Request: Send a blank email to php-notes+get-67902@lists.php.net to get a copy of this message
Often you want to split CSV-Like data, so this is the function for this :) It splits data formatted like: 1,2,3 -> [1,2,3] 1 , 3, 4 -> [1,3,4] one; two;three -> ['one','two','three'] "this is a string", "this is a string with , and ;", 'this is a string with quotes like " these', "this is a string with escaped quotes \" and \'.", 3 -> ['this is a string','this is a string with , and ;','this is a string with quotes like " these','this is a string with escaped quotes " and '.',3] function quotesplit($s) { $r = Array(); $p = 0; $l = strlen($s); while ($p < $l) { while (($p < $l) && (strpos(" \r\t\n",$s[$p]) !== false)) $p++; if ($s[$p] == '"') { $p++; $q = $p; while (($p < $l) && ($s[$p] != '"')) { if ($s[$p] == '\\') { $p+=2; continue; } $p++; } $r[] = stripslashes(substr($s, $q, $p-$q)); $p++; while (($p < $l) && (strpos(" \r\t\n",$s[$p]) !== false)) $p++; $p++; } else if ($s[$p] == "'") { $p++; $q = $p; while (($p < $l) && ($s[$p] != "'")) { if ($s[$p] == '\\') { $p+=2; continue; } $p++; } $r[] = stripslashes(substr($s, $q, $p-$q)); $p++; while (($p < $l) && (strpos(" \r\t\n",$s[$p]) !== false)) $p++; $p++; } else { $q = $p; while (($p < $l) && (strpos(",;",$s[$p]) === false)) { $p++; } $r[] = stripslashes(trim(substr($s, $q, $p-$q))); while (($p < $l) && (strpos(" \r\t\n",$s[$p]) !== false)) $p++; $p++; } } return $r; } ---- Manual Page -- http://www.php.net/manual/en/function.split.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41426 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41426&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41426&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67902) next »