0

次の内容の1.txtというテキストファイルがあるとします。

wow<br>wow<br>wow<!--Read More--><br>wow<br>wow<br>wow<br>wow<br>wow<br>wow<br>

<!--Read More--> 現在、fopenコマンドを使用してテキストファイル全体を読み取って表示するまでの内容のみを表示したい。

$file_handle = fopen("posts/1.txt", "r");
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
print $line_of_text;
}

親切に誰かがこれを手伝ってくれます...

4

2 に答える 2

0
$file_handle = fopen("posts/1.txt", "r");
while ((!feof($file_handle) && (($line_of_text = fgets($file_handle)) != "<!--Read More-->")) 
{
  print $line_of_text;
}
于 2013-02-06T11:23:54.393 に答える
0

警告: これは、「停止テキスト」が常に同じ行にある場合にのみ機能します

strstr()関数を使用して、読み取った行に停止する文字列が含まれているかどうかを確認できます。

行を最初のパラメーターとして呼び出すと、検索された文字列が行にない場合、または検索された文字列のの行の部分がtrue返される場合、2 番目および 3 番目のパラメーターとして検索された文字列が返されます。false

$file_handle = fopen("posts/1.txt", "r");
while (!feof($file_handle)) {
    /* Retrieve a line */
    $line_of_text = fgets($file_handle);
    /* Check if the stop text is in the line. If no returns false
       else return the part of the string before the stop text */
    $ret = strstr($line_of_text, "<!--Read More-->", true);
    /* If stop text not found, print the line else print only the beginning */
    if (false === $ret) {
        print $line_of_text;
    } else {
        print $ret;
        break;
    }
}
于 2013-02-06T11:24:58.817 に答える