strtok issues

From: Date: Tue, 27 May 2003 03:58:49 +0000
Subject: strtok issues
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969353925@lists.php.net to get a copy of this message
It seems there is much confusion about the proper function of strtok. One thing that totally puzzles me is the change in function in version 4.1. Note in the documentation: "The behavior when an empty part was found changed with PHP 4.1.0. The old behavior returned an empty string, while the new, correct, behavior simply skips the part of the string: " I don't understand how skipping a validly tokenized string can be considered 'correct' behaviour. Consider trying to break apart a csv file utilizing quotes around strings, a very common occurance: "178208","JOHN","DOE","","LANCASTER","PA","12345" "190398","JANE G","OED","1234 ANYSTREE","Spokane","Wa","12346" Notice the empty address line in the first line of the text. Normal processing in 'C' and PHP prior to 4.1 would be: $tok = strtok($inline,",\""); while ($tok) { $inrec[$x] = strtr ($tok,"'"," "); $tok = strtok($inline,",\""); }; This would render an array $inrec, and $inrec[3] of the first line would be an empty string, and $inrec[4] would be the city. But the new functionality causes $inrec[3] to be the city. The current 'correct' behaviour doesn't behave like 'C', and is rendered useless in one of the most commonly used applications for strtok. The work around for this is substantial lines of code. 'Split' doesn't appear to work for this, it has a tendancy to carry one of the delimiters into a string. I have tried: $inrec = split(",\"",$inline); and $inrec = split("\",\"",$inline); One carries the last quote into the last array instance (e.g. 12345") The second carries the first quote into the first array instance (e.g. "178208) I guess I don't understand the rational used to break with traditional processing of strtok. How can an empty string be considered not important in processing under logical processes? Cameron Lauseng

« previous php.doc (#969353925) next »