2011-08-08 4 views
2

テキストがたくさんあるテキストファイルがあり、その一部をPHPで画面に表示したい。PHPでテキストファイルを読み込んで、特定の文字列の後にすべてのテキストを表示する

、ある時点で私は、ファイルの最後に(ちょうどそれの後に)この文字列からすべてのコンテンツを取得したい何ITEM DESCRIPTION:

のような文字列があります。

これは、これまでの私のコードです:

$file = "file.txt"; 
$f = fopen($file, "r"); 
while ($line = fgets($f, 1000)) 
    echo $line; 

:)

答えて

4
$file = "file.txt"; 
$f = fopen($file, 'rb'); 
$found = false; 
while ($line = fgets($f, 1000)) { 
    if ($found) { 
     echo $line; 
     continue; 
    } 
    if (strpos($line, "ITEM DESCRIPTION:") !== FALSE) { 
     $found = true; 
    } 
} 
+0

私は空の画面を表示します – Alex

+0

Woops。ごめんなさい。 strpos引数が逆になります。私は答えを編集します。 –

+0

ok私はstrposの引数を逆にして、今すぐに動作します – Alex

1

$file = "file.txt"; 
$f = fopen($file, "r"); 
$start = false; 
while ($line = fgets($f, 1000)) { 
    if ($start) echo $line; 
    if ($line == 'ITEM DESCRIPTION') $start = true; 
} 

について?

3

strstr()とfile_get_contents()はどうやって使用しますか?

$contents = strstr(file_get_contents('file.txt'), 'ITEM DESCRIPTION:'); 
# or if you don't want that string itself included: 
$s = "ITEM DESCRIPTION:"; # think of newlines as well "\n", "\r\n", .. or just use trim() 
$contents = substr(strstr(file_get_contents('file.txt'), $s), strlen($s)); 
関連する問題