Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
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?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

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);
about 4 years ago · Santiago Trujillo Denunciar

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.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda