note 61925 added to function.explode

From: Date: Wed, 15 Feb 2006 10:29:26 +0000
Subject: note 61925 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-103758@lists.php.net to get a copy of this message
My version looks like the following: <?php /** * Explode a string by delimiters, but ignore delimiters inside encapsulation. * * This function works like the explode() function, but takes care that * delimiters inside encapsulated parts are ignored. This is useful for CSV * files, where the parts may be encapsulated by " or ', and any delimiter * between two encapsulation characters is part of the string, so this part * cannot be exploded. * Note however, when reading data from a CSV file, the PHP function fgetcsv() * should be used. * * @param string $delim is the delimiter that should be used for splitting. * @param string $string is the string that should be splitted. * @param string $encap is the encapsulation character. * * @return array of string that contains the splitted parts. */ function quote_explode( $delim, $string, $encap = '"' ) { $parts = explode( $delim, $string ); $correctParts = array(); $correctIndex = 0; $insideEncaps = false; foreach ( $parts as $part ) { $numEncaps = substr_count( $part, $encap ); if ( !$insideEncaps ) { switch ( $numEncaps ) { case 1: $correctParts[$correctIndex] = str_replace( $encap, '', $part ); $insideEncaps = true; break; case 0: case 2: $correctParts[$correctIndex++] = str_replace( $encap, '', $part ); break; default: echo 'Found more than 2 encapsulations - this should not happen!'; } } else { switch ( $numEncaps ) { case 0: $correctParts[$correctIndex] .= ';'.str_replace( $encap, '', $part ); break; case 1: $correctParts[$correctIndex++] .= ';'.str_replace( $encap, '', $part ); $insideEncaps = false; break; default: echo 'Found more than 2 encapsulations - this should not happen!'; } } } return $correctParts; } ?> However, I just replaced it with fgetcsv(). There really should be a see-also link to the fgetcsv() function. ;-)) ---- Server IP: 64.246.30.37 Probable Submitter: 84.114.180.113 (proxied: 127.0.0.1) ---- Manual Page -- http://www.php.net/manual/en/function.explode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61925 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61925&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61925&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61925&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61925&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61925&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61925&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61925&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61925&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#103758) next »