douzhan8395 2016-05-06 15:29
浏览 16

从文件php中读取可变行数[关闭]

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!

  • 写回答

1条回答 默认 最新

  • doumouyi4039 2016-05-06 15:31
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题