2016-05-06 7 views
-4

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> 

を持っている私は私の入力は、関数の変数としてであろうとします。私はそれをどのように機能させるのですか?ありがとう!

+3

あなたは何を試してみましたか? – DevDonkey

答えて

1

[戻る]で文字列を爆発させる必要があります。

$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++は、その時点で1でカウントアップすることを意味します。

そして出力にエコーは行番号$i

EDITがあります:私はあなたがやろうとしているかわかりません。今$結果

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

と$結果2は、各変数(pavadinimas /文字列)の25/50行です:しかし、これはあなたのコードの私の最高の推測です。

あなたは私があなたが欲しいものに進むために多くを与えていない、あなたのコードは私が理解しているものを超えています。しかしこれはおそらくあなたが望むものです。

+0

また、ファイルを開き、 'fopen'を使ってファイルを1行ずつ読むこともできます。 – Styphon

+0

@Styphonあなたはそれを配列に読み込んでもよろしいですか?私はそれを記憶しておらず、それが何かを見つけることができません。 – Andreas

+0

ポジティブ - http://php.net/manual/en/function.fgets.php#example-2602 – Styphon

関連する問題