Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Read contents of a file until a string...

    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:
    Code:
    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
    Last edited by Evlich; 03-08-2003 at 07:32 PM.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    PHP Code:

    strpos
    ($contents"</code:cpp>") === false 
    is that some kind of new equality operator you've discovered? ...only if it really absolutely equals false?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •