file_get_contents 有时找不到文本

发布于 2024-11-28 08:55:49 字数 406 浏览 0 评论 0原文

我是 PHP 新手,所以请保持友善:)

有时 file_get_contents 会完成其工作,有时则不会。我在网页上构建了一个简单的 URL 检查(如果存在)。但问题是,即使 URL 确实存在(手动检查源代码),file_get_contents 和 preg_match 也找不到它。我不明白为什么。代码如下:

$page = $URL_that_should_be_checked;
$checkurl = str_replace("/", "\/", $checkurl);
$page = file_get_contents($userpage);
$checkurl = "/".$checkurl."/";
$report = preg_match($checkurl, $page);

非常感谢!!

I'am new to PHP, so please be nice :)

Sometimes file_get_contents does its job and sometimes not. I have build a simple check for URLs, if they exist, on webpages. But the problem is, that even the URL exist for sure (manually checked the sourcecode), file_get_contents and preg_match do not find it. I can't figure out why. Here's the code:

$page = $URL_that_should_be_checked;
$checkurl = str_replace("/", "\/", $checkurl);
$page = file_get_contents($userpage);
$checkurl = "/".$checkurl."/";
$report = preg_match($checkurl, $page);

Thank you very much!!

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

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

发布评论

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

评论(4

烟燃烟灭 2024-12-05 08:55:49

试试这个:

$page = file_get_contents($userpage);

$report = preg_match('/' . preg_quote($checkurl) . '/', $page);

Try this:

$page = file_get_contents($userpage);

$report = preg_match('/' . preg_quote($checkurl) . '/', $page);
贩梦商人 2024-12-05 08:55:49

遵循您的代码相当困难。

您是否检查了文档页面并注意到特殊字符可能需要使用 urlencode() 函数进行编码?

在此行 $checkurl = str_replace("/", "\/", $checkurl); 变量 $checkurl 似乎没有定义。

在这一行 $userpage 似乎没有定义。您提供的代码中仅定义了 $page

看起来您做了很多工作来设置 preg_match 及其值 $report 。目前尚不清楚为什么在此过程中需要获取页面。

另外,您是否设置了 allow_url_fopen为真?您收到任何错误消息吗?

It's rather difficult to follow your code.

Did you check the documentation page and note that special characters may need to be encoded with the urlencode() function?

On this line $checkurl = str_replace("/", "\/", $checkurl); the variable $checkurl does not appear to have a definition.

On this line $userpage does not seem to be defined. Only $page is defined in the code you've provided.

It looks like you're doing a lot of work to set up a preg_match and $report its value. It's unclear why you need to fetch the page during this process.

Also, do you have allow_url_fopen set to true? Are you getting any error messages?

病毒体 2024-12-05 08:55:49

您应该使用 PHP 函数 preg_quote 来解析 preg_match 函数的 URL。示例:

$checkurl = preg_quote($checkurl);

请参阅: http://php.net/manual/de/function.preg -quote.php

You should use the PHP Function preg_quote to parse the URL to your preg_match Function. Example:

$checkurl = preg_quote($checkurl);

See: http://php.net/manual/de/function.preg-quote.php

深居我梦 2024-12-05 08:55:49

不确定你的代码在做什么。

$page = $URL_that_should_be_checked;

只需执行两行,

$page = file_get_contents($userpage);

就可以在没有设置 $userpage 的情况下执行此操作。

Not sure what your code is doing.

You do

$page = $URL_that_should_be_checked;

but two lines later do

$page = file_get_contents($userpage);

without ever having set $userpage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文