douxian4323 2011-05-02 19:39
浏览 34

搜索特定字符串的远程网页

I would like to create a PHP script that will go to another website (given a URL) and check the page source of that page for a certain string of data.

I actually have a way of doing it right now, but looking for an alternative way.

Right now I'm using the file_get_contents php function to read in the page source of the URL into a variable.

$link = "www.example.com";
$linkcontents = file_get_contents($link);

Then I use the strpos php function to search the page for the string I'm looking for:

$needle = "<div>find me</div>";
if (strpos($linkcontents, $needle) == false) {
echo "String not found";
} else {
echo "String found";
}

I have heard the cURL command is good for handling things that have to do with URLs, I'm just not sure how I would use it to do what I'm doing with file_get_contents and strpos functions combined like I have put above.

Or if there is another way to do it, I'm all ears :-)

  • 写回答

3条回答 默认 最新

  • dongnu4254 2011-05-02 19:42
    关注

    If file_get_contents works just fine for the task at hand, why change anything...? I say keep using it.

    Note that you'll need to pass it a URL that starts with "http://", otherwise it'll try to open a local file called "www.example.com".

    Also it's good practice to do === false with strpos, since otherwise a match at position 0 will not be recognized (since 0 == false but not 0 === false)

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类