Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:24:00+00:00 2026-06-15T18:24:00+00:00

Is there some PHP function or class that allows me to read a file

  • 0

Is there some PHP function or class that allows me to read a file like an array of characters?

For example:

$string = str_split('blabla');

$i = 0;

switch($string[$i]){

  case 'x':
    do_something();
    $i++;


  case 'y':
    if(isset($string[++$i]))
      do_something_else();
    else
      break;

   case 'z':
      // recursive call of this code etc..

}

I know that I can use $string = file_get_contents($file), but the problem is that I get a huge amount of memory used for a tiny 800K file (like 80MB).

So can I somehow “stream” the file in my code above with some kind of arrayaccess like class that automatically reads data from the file when I call isset() ? 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:24:01+00:00Added an answer on June 15, 2026 at 6:24 pm

    You can use fseek and fgetc to jump around in a file and read single characters at a time.

    // Leaves the file handle modified
    function get_char($file, $char) {
       fseek($file, $char);
       return fgetc($file);
    }
    

    You mentioned you wanted array behavior specifically. You can build a class which implements ArrayAccess to support this.

    This could be dangerous for several reasons:

    • You’ll need to guard against $char inputs that request indices past the length of the file
    • The file handle will be constantly mutated (should be okay, as long as you’re not using it elsewhere)
    • This could be inefficient (offset by caching past requests)

    A slightly more efficient alternative would be to “lazily” read the file (i.e., read it in chunks rather than all at once). Here’s some (untested) code:

    class BufferedReader {
        // The size of a chunk in bytes
        const BUFFER_SIZE = 512;
    
        protected $file;
        protected $data;
    
        function __construct($fname) {
            $this->file = fopen($fname, 'r');
        }
    
        function read_buffer() {
            $this->data .= fread($this->file, self::BUFFER_SIZE);
        }
    
        function get_char($char) {
            while ( $char >= strlen($this->data) && !feof($this->file) ) {
                $this->read_buffer();
            }
    
            if ( $char >= strlen($this->data) ) {
                return FALSE;
            }
    
            return substr($this->data, $char, 1);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a php function or class that does not require special libraries to
I heard that in PHP there are some alternatives for the functions substr() and
there are some code as this: <div class=test> <div class=upsell-tags> <?php echo $this->getChildHtml('product_additional_data') ?>
I'm using PHP in order to redirect some search query. There is some example
I there a way to compare some string in php with this kind of
I am trying some things with JSON and PHP and there is something that
I am reading some PHP code that I could not understand: class foo {
I develop some PHP project on Linux platform. Are there any disadvantages of putting
I am looking at some PHP code where there are translation strings. For French
We run a number of web applications written in PHP. Unfortunately there are some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.