file_get_contents 有时找不到文本
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
Try this:
遵循您的代码相当困难。
您是否检查了文档页面并注意到特殊字符可能需要使用 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?
您应该使用 PHP 函数 preg_quote 来解析 preg_match 函数的 URL。示例:
请参阅: 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:
See: http://php.net/manual/de/function.preg-quote.php
不确定你的代码在做什么。
您
只需执行两行,
就可以在没有设置 $userpage 的情况下执行此操作。
Not sure what your code is doing.
You do
but two lines later do
without ever having set $userpage.