如何检查变量中是否存在此代码

时间:2014-02-11 18:32:35

标签: php curl

我正在查看Google在PHP中的CURL结果,现在有时会导致使用此格式的重定向:

 Please click <a href="https://www.google.com/search?q=site:test.com//&gbv=1&sei=vGn6UobNMaqssQTo4IHoDA">here</a> if you are not redirected

如何检查这是否在名为$ result的变量中,然后使用PHP获取链接href中的URL?

2 个答案:

答案 0 :(得分:0)

使用DomDocument解析HTML并提取网址。请注意,如果您使用的脚本与要求提供数据的服务器的ToS相反,则可能会产生法律和财务后果。

我正在使用strpos在提交资源之前快速检查我需要解析页面的提示。

if (strpos($result, 'if you are not redirected') !== false) {   

    $doc = new DOMDocument();
    @$doc->loadHTML($result);
    $newUrl = $doc->getElementsByTagName('a')->item(0)->getAttribute('href');

    // new request using $newUrl
}

文档

答案 1 :(得分:-1)

if(isset($result )){
    $url = preg_replace('/.*Please click <a href="([^"]*)".*/', "$1", $result );
    print $url;
}

if(isset($result ) && preg_match('/Please click <a href="([^"]*)"/', $result, $m )){
    print $m[1];
}
相关问题