...

View Full Version : Read contents of a file until a string...



Evlich
03-08-2003, 08:27 PM
Hello, I am working on a script and what i need to do is to read the contets of the file into a string until a certain string occurs. Here is what I have for my code right now:


while( $contents .= fread($fp, 46) && strpos($contents, "</code:cpp>") === false );
if( !strpos($contents, "</code:$tag>") )
{
$current = substr($contents, strpos($contents, "</code:$tag>"), strlen($contents));
$contents = substr($contents, 0, strpos($contents, "</code:$tag>"));
}
else
{
$current = substr($contents, strpos($contents, "</$tag>"), strlen($contents));
$contents = substr($contents, 0, strpos($contents, "</$tag>"));
}

At the time that the segment of code is run, the file looks like:
#include <string>
#include <iostream.h>
class game
{
string name;
}
void main(int argv, char *argc[])
{
game Str;
Str.name = "Hello World";
cout<<Str.name;
}
</code:cpp>

and $tag has a value of "cpp". The error is just that the maximum time limit is exceeded, so i am guessing that the loop is an infinite loop, but I can't figure out why exactly. Can someone help me, I thought that there was a function to do this, but I couldn't find it in the help files. Thanks a lot.
~evlich

Spookster
03-08-2003, 08:40 PM
strpos($contents, "</code:cpp>") === false



is that some kind of new equality operator you've discovered? ...only if it really absolutely equals false? :)

Evlich
03-08-2003, 09:04 PM
I really don't know, it is what I got out of the PHP manual:
// in PHP 4.0b3 and newer:
$pos = strpos($mystring, "b");
if ($pos === false) { // note: three equal signs
// not found...
}
Should I use the other one:
// in versions older than 4.0b3:
$pos = strpos($mystring, "b");
if (!is_integer($pos)) {
// not found...
}
I am using PHP version 4.3.
~evlich

Evlich
03-08-2003, 11:25 PM
Ok, here is an idea, the file is opened in a different file and then this file is included and I try to read from the file, can you do that? I am passing the file pointer $fp by reference to this file. I echoed the value of $contents each time that I went through the loop and it seems like the problem is that it is not reading any more from the file because $contents is always the same. Any ideas? Thanks a lot.
~evlich



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum