开发者

file_get_contents sometimes does not find texts

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!!


Try this:

$page = file_get_contents($userpage);

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


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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜