Reputation: 23
I'm trying to use curl to detect whether a piece of text exists in the source code of a remote webpage. For example, I'm trying to see if this string exists in the source:
<!-- BEGIN TEST CODE -->
If it exists in the HTML source code of a remote webpage (say, example.com) I want to echo "yes". If it doesn't exist in the source, I want it to echo "no".
This is what I've tried so far:
$ch = curl_init("http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$text = curl_exec($ch);
$test = strpos($text, "<!-- BEGIN TEST CODE -->");
if ($test==false)
{
echo "yes";
}else{
echo "no";
}
When I run it, it always outputs "yes". What's wrong in my code, and how should I do it correctly?
Upvotes: 1
Views: 12576
Reputation: 786
The two functions you need are cURL
and strpos()
.
<?php
$ch = curl_init("http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$text = curl_exec($ch);
$test = strpos($text, "<!-- BEGIN TEST CODE -->");
if ($test==false)
{
echo "no";
}
else
{
echo "yes";
}
?>
Upvotes: 8
Reputation: 1348
Have you tried using $is_a_test = strpos($the_page_text, "<!-- BEGIN TEST CODE -->");
?
PHP strpos() documentation: http://www.php.net/manual/en/function.strpos.php
Upvotes: 0