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);

所以基本上,我可以显示整个页面,或者针后的一行,但不是针后的所有内容......

我尝试使用 PHP 参考、Stackoverflow 搜索引擎,当然还有 google 来找到解决方案,但我找不到解决方案,感谢所有愿意帮助我的人。

问候用户rr3

4

1 回答 1

2

从文件中提取文本

如果您想要整个文件,则一次只能使用fgets() DOC从文件中抓取一行,然后使用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;

抓住你的文字

这可以使用 PHPs substr() DOCs函数结合strpos() DOCs来实现:

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

这将在第一次出现 needle 之后抓取所有文本Montag</b>

把它们放在一起

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