检查网页中是否存在字符串 [英] Check if string exists in a web page

查看:100
本文介绍了检查网页中是否存在字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用curl来检测远程网页的源代码中是否存在一段文本。例如,我试图查看此字符串在源中是否存在:

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

如果它存在于远程网页的HTML源代码中(例如, example.com )我想回声是。如果它在源代码中不存在,我希望它回显 no。

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";
}

当我运行它时,它总是输出 yes。 我的代码有什么问题,我应该如何正确做?

When I run it, it always outputs "yes". What's wrong in my code, and how should I do it correctly?

推荐答案

这两个函数需要的是 cURL strpos()

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";
}
?>

这篇关于检查网页中是否存在字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆