Click to See Complete Forum and Search --> : [RESOLVED] $arr=explode(',',$cookie);
gert cuykens
07-14-2006, 04:41 PM
How do i make a string that knows with , is a seperator and witch is a char , ?
First i thought doing this \, and handle the string before exploding it but then what if i have \, as input ? then i have to do somthing like \\, ? Anyway i am stuck with a infinity of \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\, ?
NogDog
07-14-2006, 06:41 PM
Are you trying to work with CSV files, or is this in a different context? (I have different answers depending on the context).
gert cuykens
07-14-2006, 06:51 PM
actualy i am working with csv cookies :D
NogDog
07-14-2006, 07:27 PM
Well, the CSV specification actually has a way of dealing with commas. If a field contains a comma, then that field must be wrapped in double quotes. Also, because of this, any field that contains a double quote must also be double-quoted and the double quotes within the field must be doubled up. So, if you have the following fields....
field 1
this is, field 2
this "is" field 3
I said, "This is field 4."
...would be formatted in the CSV file as...
field 1,"this is, field 2","this ""is"" field 3","I said, ""This is field 4."""
Optionally, you can quote a field even if not required (such as field 1).
So, one option is to write/find functions that encode your data into true CSV format and extract it back from the cookie value as needed. (I know I did this in Perl before, I'm not sure if I have any PHP functions lying around somewhere.)
Alternatively, if you want to escape "real" commas with a backslash, then you could use preg_split to extract it:
$test = 'field 1,this\, is field 2,"This\," I said\, "is field 3."';
$fields = preg_split('/(?<!\\\),/', $test);
gert cuykens
07-14-2006, 08:13 PM
So what it comes down to is
a) writing some long complicated csv functions
b) adding 2 lines of code
b please :D
NogDog
07-14-2006, 08:16 PM
So what it comes down to is
a) writing some long complicated csv functions
b) adding 2 lines of code
b please :D
As long as it's for "internal use only", so to speak, and not for export into "real" CSV format at some point.
gert cuykens
07-18-2006, 04:51 PM
seems like i am going to stick with csv afterall
regexp is a pain in the head :(
gert cuykens
07-18-2006, 04:55 PM
So would this corectly split a official csv file ?
/(?="),/
To my thinking it would until you do this "field , 1","field ""2""",field 3
so it has to be something between /[^"](?="),/ and /(?="""),/ i think :D
/[^"](?="),|(?="""),/
amen :D
It doesnt going to work does it :rolleyes:
NogDog
07-18-2006, 08:12 PM
I adapted this from a user-supplied function at http://us3.php.net/manual/en/function.fgetcsv.php :
function csv_string_to_array($str){
$expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
$results=preg_split($expr,trim($str));
$results=preg_replace("/^\"(.*)\"$/","$1",$results);
return(preg_replace('/""/', '"', $results));
}
// TEST:
$csv = 'field 1,"this is, field 2","this ""is"" field 3","I said, ""This is field 4."""';
$result = csv_string_to_array($csv);
echo "<pre>";
print_r($result);
echo "</pre>\n";
Seems to work, but caveat emptor.
gert cuykens
07-21-2006, 04:42 AM
unbelieveble it works :)
you defenatly need to be an alian to come up with this "/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/" ch** :D