How do I read specific number of lines from a txt file? For example I have a txt file which has 100 lines, and I need to print 25 or 50 lines to my website? I searched website and was not able to find how to do this using php or javascript. Thanks!
For now I have
<?php
if( isset($_GET['submit']) )
{
$pavadinimas = htmlentities($_GET['pavadinimas']);
$result = Myfunction($pavadinimas);
$string = file_get_contents("istorija.txt");
$array = explode(PHP_EOL, $string);
function Myfunction($pavadinimas){
For($i=0;$i<=$pavadinimas($array);$i++){
echo $array[$i] ."<br>
";
}
}
}
?>
<?php if( isset($result) ) echo $result; //print the result above the form ?>
<form action="administratorius.php" method ="GET" >
Įrašų skaičius:
<input type="text" name="pavadinimas" maxlength="30" value="<?php echo $form->value("pavadinimas"); ?>"> <br>
<input type="submit" name="submit" value="Prideti">
</form>
I want that my input would be as a variable for function. How do I make it work? Thanks!
You need to explode the string on [return].
$string = file_get_contents("file.txt");
$array = explode(PHP_EOL, $string);
Edit: EOL is better. Had forgot about it.
Edit2:
For($i=0;$i<=count($array);$i++){
echo $array[$i] ."<br>
";
}
This above code will output the full textfile.$i=0
means start at first line.$i<=count($array)
keep going till end of file. This can be changed to $i<=15
and you only output 15 lines.$i++
means count up with one at the time.
And then there is a echo to output the linenumber $i
EDIT: I'm not sure what you are trying to do. But this is my best guess of your code:
if( isset($_GET['submit']) ){
$pavadinimas = htmlentities($_GET['pavadinimas']);
$result = Myfunction($pavadinimas, 25); //reads 25 rows of the pavadinimas
$string = file_get_contents("istorija.txt");
$array = explode(PHP_EOL, $string);
$result2 = Myfunction($string, 50); // reads 50 rows of istorija.txt
function Myfunction($pavadinimas,$NoOfRows){
For($i=0;$i<=$NoOfRows;$i++){
$returnstr .= $pavadinimas[$i] ."<br>
"; // this appends the $returnstr with the next row
}
return $returnstr; // returns it to where the function was called.
}
}
Now $result and $result2 are 25/50 rows of each variable (pavadinimas/string).
You have not given me alot to go on to what you want, your code is beyond what I understan. But this is probably what you wanted.