获取缺少唯一标准的远程站点的某些元素


Get certain element of remote site with lack of unique criteria

抱歉,如果这看起来不清楚会解释它

我正在尝试从远程页面获取可能<iframe> <script>的某些元素,

这个远程页面有很多<iframe><script>但我需要的页面没有像特定id那样的唯一标准是它在代码中的位置,它总是在这段代码之上

<script src="http://code.jquery.com/jquery-1.5.2.min.js" type="text/javascript"></script> 

换句话说,我想要的iframescript就在jQuery引用上方的行中

我可以依靠哪些方法来获得此iframe。

$file = file('www.yoururl.html');
foreach($file as $key => $value) {
    if(strpos($value, 'http://code.jquery.com/jquery-1.5.2.min.js')) {
        echo $file[$key-1];
        break;
    };
};

您可以使用正则表达式来确保您没有在线上拾取任何其他内容:-

    preg_match('/<(iframe|script).+<'/(iframe|script)>/', $file[$key-1], $matches);
    echo $matches[0];