Hello, I need to read a txt with PHP line by line obtaining from each line text in specific positions, for example:
Linea 1: Luis Juarez
Linea 2: Paco Mendoza
Linea 3: Pepe Gonzales
I only want to extract the name that is in the first 4 positions Luis would be from position 0 to 3, the same would be with the other two, how do I make it go through the entire file and only show the positions that it requires.
This is the code that I have done, but it only gives me results with line 1, it does not go through the entire content of the file, I would greatly appreciate your collaboration, thanks
<?php
//Llamar al arcivo
$nombre=$_FILES['archivo']['name'];
$guardado=$_FILES['archivo']['tmp_name'];
if (!file_exists('archivos')) {
mkdir('archivos',0777,true);
if (file_exists('archivos')) {
if(move_uploaded_file($guardado, 'archivos/'.$nombre)){
echo "";
}else{
echo "Ocurrio un error con la carga del archivo";
}
}
}else{
if(move_uploaded_file($guardado, 'archivos/'.$nombre)){
}else{
echo "Ocurrio un error con la carga del archivo";
}
}
// Abrir el archivo en modo de sólo lectura:
$arc = fopen('archivos/'.$nombre,"r");
// Recorremos el archivo mostando el contenido de cada línea:
echo file_get_contents('archivos/'.$nombre,null,null,2,18);
fclose($arc);
?>
To read it line by line you could try this:
where, through a while() loop and the feof() function, we advance to the end of the file.
And on each loop we read each line via the fgets() function , advancing the file pointer.
And we subtract a length of 4 characters from it, starting at the beginning thanks to the substr() function