PHP strstr() 仅返回一行
首先:我真的很喜欢这个网站,我认为这是最好的编程论坛:)
现在我的问题,我尝试使用代码和注释来显示:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从文件中提取文本
您一次只能使用
fgets()
从文件中抓取一行super>DOC 如果您想要整个文件,请使用file_get_contents()
DOC 相反:抓取文本
这可以使用 PHP 来实现
substr()
DOCs 函数与strpos()
结合使用DOC:这将抓取第一次出现针
Montag
后的所有文本。把它们放在一起
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 usefile_get_contents()
DOCs instead:Grabbing your text
This can be achieved using PHPs
substr()
DOCs function combined withstrpos()
DOCs:This will grab all the text after the first occurrence of the needle
Montag</b>
.Putting it all together