PHP strstr() 仅返回一行

发布于 2024-12-28 16:38:14 字数 849 浏览 1 评论 0原文

首先:我真的很喜欢这个网站,我认为这是最好的编程论坛:)

现在我的问题,我尝试使用代码和注释来显示:

    $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 来找到解决方案,但我找不到解决方案,感谢每个愿意的人帮我。

问候 userrr3

first of all: I really love this site and I think this is the best forum for programming :)

Now to my problem, which I try to display using code and comments:

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

So basically, I'm able to display the whole page, or one line after the needle, but not everything after the needle....

I tried to find a solution using the PHP Reference, the Stackoverflow Search engine and of course google, but I couldn't find a solution, thanks for everybody willing to help me.

Greetings userrr3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亽野灬性zι浪 2025-01-04 16:38:14

从文件中提取文本

您一次只能使用 fgets() 从文件中抓取一行super>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;

抓取文本

这可以使用 PHP 来实现 substr() DOCs 函数与 strpos() 结合使用DOC

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

这将抓取第一次出现针 Montag 后的所有文本。

把它们放在一起

$file = file_get_contents($URL);
$buffer = substr($file, strpos($buffer, 'Montag</b>'));
echo $buffer;

Extracting text from the file

You are only grabbing one line at a time from the file using fgets() DOCs if you want the whole file then use file_get_contents() DOCs instead:

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

Grabbing your text

This can be achieved using PHPs substr() DOCs function combined with strpos() DOCs:

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

This will grab all the text after the first occurrence of the needle Montag</b>.

Putting it all together

$file = file_get_contents($URL);
$buffer = substr($file, strpos($buffer, 'Montag</b>'));
echo $buffer;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文