file_get_contents sometimes does not find texts
An answer to this question on Stack Overflow.
Question
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!!
Answer
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?