Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda