di5hq
Power Member
Boas,
Precisava de um script ou algo do género... tipo, tenho um .txt com uma palavra em cada linha, e queria q uma pagina html ou php.. n sei.. n percebo mt do assunto, tirasse a primeira linha e mostrasse no site... e se possível apagar essa linha do txt. Alguém sabe como fazer isto... provavelmente até será simples.
Cumprimentos.
EDIT 1:
Pesquisando, encontrei algo e com algumas modificações, está assim:
Este código já me faz o print só da primeira linha do .txt como quero, agora gostava de saber como eliminar a primeira linha ou 'x' caracteres do ficheiro após o print.
Nota: Cada linha terá uma palavra de 10caracteres.
EDIT 2:
Já sei como apagar a 1ª linha.
EDIT 3: [maneira simples pelo Sam19]
Precisava de um script ou algo do género... tipo, tenho um .txt com uma palavra em cada linha, e queria q uma pagina html ou php.. n sei.. n percebo mt do assunto, tirasse a primeira linha e mostrasse no site... e se possível apagar essa linha do txt. Alguém sabe como fazer isto... provavelmente até será simples.
Cumprimentos.
EDIT 1:
Pesquisando, encontrei algo e com algumas modificações, está assim:
Código:
<html>
<head><title>File read txt</title></head>
<body>
<?
$f = fopen("file1.txt", "r");
if ( $line = fgets($f, 11) ) {
print $line;
}
?>
</body>
</html>
Este código já me faz o print só da primeira linha do .txt como quero, agora gostava de saber como eliminar a primeira linha ou 'x' caracteres do ficheiro após o print.
Nota: Cada linha terá uma palavra de 10caracteres.
EDIT 2:
Já sei como apagar a 1ª linha.
Código:
<?php// the file name, this can be a path also, like /path/to/myfile.txt
$fileName = "pws.txt";
// the line to delete
$lineNum = 1;
delLineFromFile($fileName, $lineNum);
function delLineFromFile($fileName, $lineNum){
// check the file exists
if(!is_writable($fileName))
{
// print an error
print "The file $fileName is not writable";
// exit the function
exit;
}
else
{
// read the file into an array
$arr = file($fileName);
}
// the line to delete is the line number minus 1, because arrays begin at zero
$lineToDelete = $lineNum-1;
// check if the line to delete is greater than the length of the file
if($lineToDelete > sizeof($arr))
{
// print an error
print "You have chosen a line number, <b>[$lineNum]</b>, higher than the length of the file.";
// exit the function
exit;
}
//remove the line
unset($arr["$lineToDelete"]);
// open the file for reading
if (!$fp = fopen($fileName, 'w+'))
{
// print an error
print "Cannot open file ($fileName)";
// exit the function
exit;
}
// if $fp is valid
if($fp)
{
// write the array to the file
foreach($arr as $line) { fwrite($fp,$line); }
// close the file
fclose($fp);
}
//echo "done";
}
?>
EDIT 3: [maneira simples pelo Sam19]
Código:
$file = 'ficheiro.txt';
$contents = file($file);
$first_line = array_shift($contents);
file_put_contents($file, $contents);
echo $first_line;
Última edição: