如何在html中搜索特定链接?

时间:2015-08-24 01:57:21

标签: php regex string

我正在使用此代码

<?PHP

  ini_set('user_agent', 'NameOfAgent (http://www.example.net)');

  $url = "http://www.example.net/somepage.html";
  if(robots_allowed($url, "NameOfAgent")) {
    $input = @file_get_contents($url) or die("Could not access file: $url");
    $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
    if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
      foreach($matches as $match) {
        echo $match[2];
      }
    }
  } else {
    die('Access denied by robots.txt');
  }
?> 

回显html页面中的所有链接地址。

是否有人知道如何修改此代码,以便代码仅列出以"sub/"开头并以/files/a.html结尾的链接。

0 个答案:

没有答案