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