donglu1881 2015-05-23 15:24
浏览 29
已采纳

循环PHP检查,直到找到网页中的文本

On pressing the submit button, I want the PHP code to check to see if a string exists within a webpage on another website.

$random = rand(100,1000);
strpos(file_get_contents('website.com/url.php?username='.$username), $random);

So, until the code has managed to find the value of $random, it won't continue with the rest of the script. The updates can take about 10 seconds.

I have tried a while script:

$random = rand(100,1000);
echo '<div data-alert class="alert-box alert">'.$random.'</div>';
$key = false;

while($key){    
if(strpos(file_get_contents('website.com/url.php?username='.$username), $random)) $key = true;
}

This doesn't seem to work.

  • 写回答

1条回答 默认 最新

  • dongshuzui0335 2015-05-23 15:26
    关注

    On pressing the submit button, I want the PHP code to check to see if a string exists within a webpage on another website.

    If I understood your question correctly, try this:

    $content = file_get_contents($url);
    if(str_replace($string, "", $content) != $content)
        echo "found";
    

    EDIT:

    do {
        $content = file_get_contents($url);
    } while(strpos($content, $string) === false);
    

    EDIT 2:

    $i = 0;
    do {
        $content = file_get_contents($url);
        $i++;
    } while(strpos($content, $string) === false && $i <= 100);
    

    EDIT 3:

    $i = 0;
    do {
        $content = file_get_contents($url);
        $i++;
    } while(strpos($content, $string) === false && $i <= 100);
    
    if($i > 100){
        echo "nothing found";
    }
    

    EDIT 4:

    $startTime = time();
    do {
        $content = file_get_contents($url);
    } while(strpos($content, $string) === false && time() - $startTime < 30);
    
    if(time() - $startTime > 30){
        echo "nothing found";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路