Re: [PHP-DEV] Function Request: csv_explode()
On December 5, 2001 01:29 am, Stig S. Bakken wrote: Splitting a CSV string into an array could be done like this too: $fields = explode(',', trim($string, '')); Hey Stig, I don't think that it is quite that simple. :) The sample code will break for unquoted values and weird combos of quotes and commas inside quote-delimited strings. --zak -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] Function Request: csv_explode()
Splitting a CSV string into an array could be done like this too: $fields = explode(',', trim($string, '')); - Stig Justin Plock wrote: I guess this would be the correct list to post this on, but I have an idea for a function that I think would be very handy to users having to deal with CSV data. Basically, I've had to write a version of the explode() function, but have it pay attention to values that are wrapped in another character. For example: the delimiter is: , and the wrapper is a: the function would work as such: this is some text,we can have a bunch of them,but, gotta, watch, out for those commas inside it would return: this is some text we can have a bunch of them but, gotta, watch, out for these commas inside below is the PHP function I have developed: /** * Does an explode but pays attention to the delimeter. * * @param $splitter - string - required delimeter to split on * @param $wrap - string - required string to wrap text areas * @param $rest - string - required data to explode * * @return array - exploded array */ function csv_explode($splitter, $wrap, $rest) { $instring = false; $field = ''; $ret = array(); while ($rest) { $first = substr($rest, 0, 1); $rest = substr($rest, 1); if ($first == $wrap) { $instring = (!$instring) ? true : false; } if (($first == $splitter) !$instring) { $ret[] = $field; $field = ''; } else { if ($first != $wrap) { $field .= $first; } } } $ret[] = $field; return($ret); } If I knew C, I would try to port this to a PHP myself, but I don't. I was just hoping someone else would. :) Thanks. -Justin Plock -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] Function Request: csv_explode()
On Wed, 5 Dec 2001, Stig S. Bakken wrote: Splitting a CSV string into an array could be done like this too: $fields = explode(',', trim($string, '')); I've had problems with CSV files exported from Excel that does not quote all fields, escapes double quotes with double quotes and puts newlines in the middle of a field: 45,Test field, with several lines This should be decoded as a two field record: 45 Test \field\,\nwith several lines So a I'm afraid that a simple split() wouldn't be able to cope with all that. Edin -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP-DEV] Function request
I think this would be nice. To be able to pull a multi-dimentional array from a mysql result. I'm working on a database abstraction module that does exactly this, and should support every database module that is loaded into php. I've already got it doing what you describe, only I return an object which includes rowcount and fieldcount as well as the 2-d array results (index and/or associative) (and I use [rownumber]["fieldname"] :-) Probably one or two weeks until it's ready for the public, and by then it should support mysql and odbc, the rest to be added in due time (or when I have the databases set up here). Cheerio, Marc. -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]