Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
Get first and last 100 characters of a text file

I have a text file and within that text file, there are just few lines but contains about 5-10 million characters. I'm using file_get_contents but because it reads the whole file so the performance become very slow and hangs sometimes when executing. Is there a more efficient way to get first and last 100 characters?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

try this to get first 100 characters:

$fp = fopen('test.txt', 'r');
$data = fread($fp, 100);
echo $data;
fclose($fp);

and get last 100 characters:

$fp = fopen('test.txt', 'r');
fseek($fp, -100, SEEK_END);
$data = fread($fp, 100);
echo $data;
fclose($fp);
over 4 years ago · Santiago Trujillo Report

0

You can use fread to specify the number of bytes that have to be read from the file.

For example:

$handle = fopen($filename, 'r');
$contents = fread($handle, 100);
fclose($handle);
echo $contents;

However, there is no way to be sure that this will be a string of 100 characters. This depends on the character encoding.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!