从文件php

时间:2016-05-06 15:29:09

标签: javascript php

如何从txt文件中读取特定行数?例如,我有一个有100行的txt文件,我需要在我的网站上打印25或50行?我搜索了网站,无法使用php或javascript找到如何做到这一点。谢谢!

现在我有

<?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>

我希望我的输入将作为函数的变量。我如何使其工作?谢谢!

1 个答案:

答案 0 :(得分:1)

你需要在[return]上爆炸字符串。

$string = file_get_contents("file.txt");
$array = explode(PHP_EOL, $string);

编辑:EOL更好。忘了它。
EDIT2:

For($i=0;$i<=count($array);$i++){
     echo $array[$i] ."<br>\n";
}

以上代码将输出完整的文本文件 $i=0表示从第一行开始 $i<=count($array)一直持续到文件结束。这可以更改为$i<=15,您只输出15行 $i++表示当时只计算一个。

然后有一个回声输出亚麻$i

编辑:我不确定你要做什么。但这是我对你的代码的最佳猜测:

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.
      }
}

现在$ result和$ result2是每个变量的25/50行(pavadinimas / string)。

你没有给我很多东西继续你想要的东西,你的代码超出了我的想法。但这可能就是你想要的。

相关问题