繁体   English   中英

如何使用php在源代码中搜索字符串?

[英]How to search in the source code for a string with php?

我努力了

<?php
  $url = $_POST['attributename'];
  $needtofind = "did not match any documents.  </p>";
  $site = file_get_contents("https://www.google.com/#q=site:$url");
  if(strpos($site, $needtofind) == false) {
    echo 'indexed';
  } else {
    echo 'not indexed';
  } 
  ob_end_clean();
?>

HTML

<div class="center-page">
  <form method="POST">
    <textarea id="float" name="attributename" value=""></textarea><br/>
    <input type="submit" value="Go" />
  </form>
</div>

代码在同一页面上。 我只是这样输入它们就更清楚了。

主要问题是默认情况下它会在屏幕indexed上告诉我。 如果我输入任何网址,它会说indexed 例如,我在textarea jhbsadhbahsd545.com中键入url,它返回indexed时应返回not indexed 我做错了什么?

strpos可以返回0,这是一个假值。 与===比较

strpos($site, $needtofind) === false

但是我相信这不会起作用,因为Google没有返回您正在寻找的第一个响应的字符串,而是在页面加载了javascript后延迟加载。

打开Chrome和view-source:https://www.google.com/#q=site:hopefullythisisadomainthatdoesnotexists.com查看Google返回的内容以及为什么它总是丢失。


同时更改您发出请求的URL:

https://www.google.com/#q=site:$url

至:

https://www.google.com/search?q=site:$url

所以你不能以这种方式从谷歌中删除内容,他们实际上禁止你这样做。 您需要利用他们的API来完成您的需求。

https://developers.google.com/custom-search/json-api/v1/overview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM