【问题标题】:Extract all urls Href php [duplicate]提取所有网址 Href php [重复]
【发布时间】:2011-07-12 20:54:46
【问题描述】:

可能重复:
Finding links matching given string in xpath/domdocument query

您好,我有一个包含许多链接的 HTML。我目前能够获得链接,只是全部,我只会得到某个词。

$dom = new DOMDocument; $dom->loadHTML($html); $links = $dom->getElementsByTagName('a'); foreach ($links as $link){ echo $link->getAttribute('href'); }

我只会列出包含某个单词的链接, 例如:sendspace.com

结果或多或少低于:
http://www.fileserve.com/file/eDpDMm9sad/
http://www.fileserve.com/file/7s83hjh347/

然后我会将这些链接转换为 sha1。

转换后保存已应用于包含单词的链接的html sha1。

【问题讨论】:

  • 我只会列出包含某个单词的链接,例如:fileserve.com

标签: php dom hyperlink extract href


【解决方案1】:

使用phpQuery,您可以遍历DOM并找到包含您想要的href属性的锚点(<a>):

$dom = phpQuery::newDocument($htmlSource);
$anchors = $dom->find('a[href|=sendspace.com]');

$urls = array();

if($anchors) {
  foreach($anchors as $anchor) {
    $anchor = pq($anchor);
    $urls[] = $anchor->attr('href');
  }
}

【讨论】:

    【解决方案2】:

    您可以使用正则表达式来匹配字符串中的单词(或其他任何内容),如下所示:

    foreach ($links as $link) {
        if (preg_match("/example\.com/i", $link->getAttribute('href'))) {
            // do things here!
        }
    }
    

    【讨论】:

    • 如何将这些链接转换为 sha1?然后返回到已经用sha1应用的html
    • 您可以使用 sha1 使用此函数对其进行哈希处理:sha1($thingyouwanttohashtosha1);
    猜你喜欢
    • 2011-07-12
    • 2014-08-29
    • 2011-04-21
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2012-08-11
    • 2013-05-05
    相关资源
    最近更新 更多