1

まず第一に、私はこのサイトが大好きで、プログラミングに最適なフォーラムだと思います :)

ここで、コードとコメントを使用して表示しようとする私の問題に進みます。

    $file = fopen ($URL, "r");
    // $URL is a string set before, which is correct
    // Also, I got the page-owners permission to acquire the page like that
    if (!$file) 
    {
        echo "<p>Could not open file.\n";
        exit;
    }
    while (!feof ($file)) 
    {
        $buffer = fgets($file);
        $buffer= strstr($buffer, "Montag</b>"); 
        // If I don't use this line, the whole page gets displayed...
        // If I use this line, only the first line after the needle gets displayed
        echo $buffer;
    }
    fclose($file);

基本的に、ページ全体、または針の後の 1 行を表示できますが、針の後のすべてではありません....

PHP リファレンス、Stackoverflow 検索エンジン、そしてもちろん Google を使用して解決策を見つけようとしましたが、解決策を見つけることができませんでした。

こんにちは userrr3

4

1 に答える 1

2

ファイルからのテキストの抽出

ファイル全体が必要な場合は、fgets() DOCを使用してファイルから一度に 1 行だけを取得し、代わりにfile_get_contents() DOCを使用します。

$file = file_get_contents($URL);
$buffer= strstr($file, "Montag</b>"); 
// If I don't use this line, the whole page gets displayed...
// If I use this line, only the first line after the needle gets displayed
echo $buffer;

テキストを取得する

これは、PHP のsubstr() DOCs関数とstrpos() DOCsを組み合わせて使用​​することで実現できます。

$buffer = substr($buffer, strpos($buffer, 'Montag</b>'));

これにより、最初に針が出現した後のすべてのテキストが取得されますMontag</b>

すべてを一緒に入れて

$file = file_get_contents($URL);
$buffer = substr($file, strpos($buffer, 'Montag</b>'));
echo $buffer;
于 2012-01-21T11:44:24.570 に答える