Javascript 从php文件读取可变行数
如何从txt文件中读取特定数量的行?例如,我有一个txt文件,有100行,我需要打印25或50行到我的网站?我搜索了这个网站,但找不到如何使用php或javascript实现这一点。谢谢 现在我有Javascript 从php文件读取可变行数,javascript,php,Javascript,Php,如何从txt文件中读取特定数量的行?例如,我有一个txt文件,有100行,我需要打印25或50行到我的网站?我搜索了这个网站,但找不到如何使用php或javascript实现这一点。谢谢 现在我有 <?php if( isset($_GET['submit']) ) { $pavadinimas = htmlentities($_GET['pavadinimas
<?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>\n";
}
}
}
?>
<?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>
Įrašskaičius:
您需要在[return]上分解字符串
$string = file_get_contents("file.txt");
$array = explode(PHP_EOL, $string);
编辑:下线更好。我已经忘了。
编辑2:
现在$result和$result2是每个变量(pavadinimas/string)的25/50行
你没有给我太多去做你想要的,你的代码超出了我的理解。但这可能是你想要的 您能告诉我们您尝试了什么吗?或者您可以打开文件并使用fopen
@Styphon逐行读取它您确定它读取到一个数组吗?我没有这方面的记忆,也找不到它所做的任何事情。正-file()是将整个文件读入数组的本机函数,例如$lines=file('file.txt');谢谢,但我对代码还不熟悉,所以我如何设置我想要看到的特定行数?谢谢
For($i=0;$i<=count($array);$i++){
echo $array[$i] ."<br>\n";
}
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>\n"; // this appends the $returnstr with the next row
}
return $returnstr; // returns it to where the function was called.
}
}