treat large string like file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    treat large string like file

    So, I've got some PHP code that reads in a file and parses some data as
    it goes along.
    However, now I'm thinking it might be easier for me to store chunks of
    the file in the database rather than all the bits of data, so that it
    can be interpreted in different ways if need be. But the problem is,
    now I can't just use fread to read in two bytes of data, because I've
    already got the whole chunk stored in a variable. And I really don't
    want to deal with tons of substrings, it would be much easier to have a
    sort of file pointer. So is there any way, or any set of functions
    that I can use to deal with this?

  • petersprc

    #2
    Re: treat large string like file

    To get the data two chars at a time, you could do:

    $len = strlen($data);
    for ($i = 0; $i < $len; $i += 2) {
    $str = substr($data, $i, 2);
    echo "String is \"$str\".\n" ;
    }

    To access using a streams interface, you could dump the data into a
    temporary file, then read it in. Or create a stream wrapper with
    stream_wrapper_ register.

    Mark wrote:
    So, I've got some PHP code that reads in a file and parses some data as
    it goes along.
    However, now I'm thinking it might be easier for me to store chunks of
    the file in the database rather than all the bits of data, so that it
    can be interpreted in different ways if need be. But the problem is,
    now I can't just use fread to read in two bytes of data, because I've
    already got the whole chunk stored in a variable. And I really don't
    want to deal with tons of substrings, it would be much easier to have a
    sort of file pointer. So is there any way, or any set of functions
    that I can use to deal with this?

    Comment

    • Mark

      #3
      Re: treat large string like file


      petersprc wrote:
      To get the data two chars at a time, you could do:
      >
      $len = strlen($data);
      for ($i = 0; $i < $len; $i += 2) {
      $str = substr($data, $i, 2);
      echo "String is \"$str\".\n" ;
      }
      >
      To access using a streams interface, you could dump the data into a
      temporary file, then read it in. Or create a stream wrapper with
      stream_wrapper_ register.
      Well, the two characters at a time thing doesn't work very well,
      because it's not always just two characters, and I've been doing a lot
      of fseek-ing. The temp file solution could work, but that just sounds
      like a horrible thing to do. Thanks anyway.

      Comment

      • Pedro Graca

        #4
        Re: treat large string like file

        Mark wrote:
        So, I've got some PHP code that reads in a file and parses some data as
        it goes along.
        However, now I'm thinking it might be easier for me to store chunks of
        the file in the database rather than all the bits of data, so that it
        can be interpreted in different ways if need be. But the problem is,
        now I can't just use fread to read in two bytes of data, because I've
        already got the whole chunk stored in a variable. And I really don't
        want to deal with tons of substrings, it would be much easier to have a
        sort of file pointer. So is there any way, or any set of functions
        that I can use to deal with this?
        Index into the whole chunk?

        $two_chars = substr($whole_c hunk, 17655, 2);
        switch ($two_chars) {
        case 'C3': $three_chars = substr($whole_c hunk, 17657, 3);
        break;
        case '--': $three_chars = '---';
        break;
        default: $three_chars = substr($whole_c hunk, 17655, 3);
        break;
        }

        --
        I (almost) never check the dodgeit address.
        If you *really* need to mail me, use the address in the Reply-To
        header with a message in *plain* *text* *without* *attachments*.

        Comment

        • Mark

          #5
          Re: treat large string like file


          Pedro Graca wrote:
          Mark wrote:
          So, I've got some PHP code that reads in a file and parses some data as
          it goes along.
          However, now I'm thinking it might be easier for me to store chunks of
          the file in the database rather than all the bits of data, so that it
          can be interpreted in different ways if need be. But the problem is,
          now I can't just use fread to read in two bytes of data, because I've
          already got the whole chunk stored in a variable. And I really don't
          want to deal with tons of substrings, it would be much easier to have a
          sort of file pointer. So is there any way, or any set of functions
          that I can use to deal with this?
          >
          Index into the whole chunk?
          >
          $two_chars = substr($whole_c hunk, 17655, 2);
          switch ($two_chars) {
          case 'C3': $three_chars = substr($whole_c hunk, 17657, 3);
          break;
          case '--': $three_chars = '---';
          break;
          default: $three_chars = substr($whole_c hunk, 17655, 3);
          break;
          }
          >
          --
          I (almost) never check the dodgeit address.
          If you *really* need to mail me, use the address in the Reply-To
          header with a message in *plain* *text* *without* *attachments*.
          Err.. I think the best solution would be for me to make some sort of
          wrapper class. Which deals with all the substr-ing and returns
          whatever I ask for... so I can set up a function like

          sseek($my_strin g_chunk_object, 2); // string seek

          which returns 2 (or however many) characters from the string and
          advances the internal pointer.. (which would probably just an integer
          to be used in the substr function)

          then I could set up wrappers for how the data should be interpreted too
          while I'm at it...

          Comment

          Working...