Re: PHP 4.0 Bug #6841 Updated: split, explode, strtok not parsing correctly
| From: | Rasmus Lerdorf | Date: | Wed, 18 Oct 2000 18:36:29 +0000 |
| Subject: | Re: PHP 4.0 Bug #6841 Updated: split, explode, strtok not parsing correctly | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-35452@lists.php.net to get a copy of this message | ||
By simple we mean a single call to a single function with a given set of
arguments that is not returning what you would expect.
-Rasmus
On 18 Oct 2000 swenson@heronetwork.com wrote:
> ID: 6841
> User Update by: swenson@heronetwork.com
> Status: Feedback
> Bug Type: Scripting Engine problem
> Description: split, explode, strtok not parsing correctly
>
> I already simplified it down and added a bunch of echo's so you can see the walk through.
>
> 1) Place the sample input into a file called 'mime.txt'.
> 2) Place the php script into a file in the same directory.
> 3) Open the example script. I put a simple call in it already.
>
> The events are pretty straight forward:
> 1) Open a file
> 2) Read a line into an array of tokens
> 3) skip the first token (it is the mime type)
> 4) check to see if the pased in arg matches any of the other tokens
> 5) return the first token from a line with a matching string or default to
> "application/binary"
>
> The extra comments in the script are for other regular expressions that SHOULD work but do not.
>
> Previous Comments:
> ---------------------------------------------------------------------------
>
> [2000-10-18 06:09:44] stas@php.net
> Could you please provide short example of any of these functions working wrong - as a short
> isolated script, not as a part of complicated code?
>
> ---------------------------------------------------------------------------
>
> [2000-09-21 22:55:42] swenson@heronetwork.com
> On current build I am trying to parse an external tab and
> space separated file (mime types). All of the tokenizing functions appear to not be performing
> the regex parsing correctly. All of these functions are performing very odd. Most interesting is
> that the first item is not even getting into the arrays (or token lists).
>
> Example input:
> # Simple mime file
> text/html htm HTM html HTML shtml SHTML
> text/plain java JAVA c C cc CC cpp CPP h H txt TXT
> text/rtf rtf RTF
>
> Script:
> <?php
> $mimeFile="/tmp/mime.txt";
>
> function get_mime_type ( $ext ) {
> global $mimeFile;
> if( !$ext || strlen(trim($ext)) == 0 ) {
> return "application/binary";
> }
> $fp = fopen ($mimeFile, "r");
> while(!feof($fp)) {
> $next = fgetss($fp, 300);
> $next = trim($next);
> echo("<p>Line in: $next<br>");
> if( !$next ) continue;
> // try it with [ts] or [:space:] or " " etc.
> // it just has problems, same with split and strtok $mime = explode("[
> ]",$next);
> if( substr($mime[0], 0, 1) == "#" ) continue;
> $len = count($mime);
> echo("Line has: $len tokens - ");
> for( $x = 1 ; $x <= $len ; $x++ ) {
> if( !$mime[$x] ) continue;
> if( substr($mime[$x] ,0 , 1) == "#" ) break;
> echo("$x - $mime[$x], ");
> if( $ext == $mime[$x] ) return $mime[0];
> }
> }
> return "application/binary";
> }
> echo("<br><H1>Mime type for .H is " . get_mime_type(".H") .
> "</H1>");
> ?>
>
> Sample output:
> Line in: # Simple mime file
> Line in:
> Line in: text/html htm HTM html HTML shtml SHTML
> Line has: 6 tokens - 1 - HTM, 2 - html, 3 - HTML, 4 - shtml, 5 - SHTML,
> Line in: text/plain java JAVA c C cc CC cpp CPP h H txt TXT
> Line has: 12 tokens - 1 - JAVA, 2 - c, 3 - C, 4 - cc, 5 - CC, 6 - cpp, 7 - CPP, 8 - h, 9 - H,
> 10 - txt, 11 - TXT,
> Line in: text/rtf rtf RTF
> Line has: 2 tokens - 1 - RTF,
> Line in:
> Mime type for .H is application/binary
>
> ---------------------------------------------------------------------------
>
>
> Full Bug description available at: http://bugs.php.net/?id=6841
>
>
>