【问题标题】:How do I extract links with a specific domain name using PHP and Regex?如何使用 PHP 和 Regex 提取具有特定域名的链接?
【发布时间】:2015-12-04 17:06:20
【问题描述】:

我正在尝试从包含 HTML 的数据库列中提取包含 www.domain.com 的 url。正则表达式必须过滤掉 www2.domain.com 实例和外部 URL,如 www.domainxyz.com。它应该只搜索正确编码的锚链接。

这是我目前所拥有的:

<?php
    $content = '<html>
    <title>Random Website</title>
    <body>
        Click <a href="http://domainxyz.com">here</a> for foobar
        Another site is http://www.domain.com
        <a href="http://www.domain.com/test">Test 1</a>
        <a href="http://www2.domain.com/test">Test 2</a>
        <Strong>NOT A LINK</strong>
    </body>
    </html>';

    $regex = "((https?)\:\/\/)?";
    $regex .= "([a-z0-9-.]*)\.([a-z]{2,4})"; 
    $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";
    $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?";
    $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; 
    $regex .= "([www\.domain\.com])";

    $matches = array(); //create array
    $pattern = "/$regex/";

    preg_match_all($pattern, $content, $matches); 

    print_r(array_values(array_unique($matches[0])));
    echo "<br><br>";
    echo implode("<br>", array_values(array_unique($matches[0])));
?>

我正在寻找这个仅查找和输出http://www.domain.com/test。

如何修改我的正则表达式来完成此操作?

【问题讨论】:

  • 基于 DOMDocument 和 DOMXPath 的解决方案怎么样?我看你只是提取 href 属性值,对吧?
  • 谢谢,我考虑过这个,但如果从数据库查询中获取 html,这样的解决方案可行吗?
  • 请查看this code。我建议在这里只使用正则表达式作为最后的手段。

标签: php html regex url


【解决方案1】:

这是一种更安全的方法来提取包含www.domain.com 的a href 属性值,其中键是XPath '//a[contains(@href, "www.domain.com")]':

$html = "YOUR_HTML_STRING"; // Your HTML string
$dom = new DOMDocument;    
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$arr = array();
$links = $xpath->query('//a[contains(@href, "www.domain.com")]');

foreach($links as $link) { 
   array_push($arr, $link->getAttribute("href"));
}

print_r($arr);

见IDEONE demo,结果:

Array
(
    [0] => http://www.domain.com/test
)

如您所见,您也可以将 DOMDocument 和 DOMXPath 与字符串一起使用。

代码是不言自明的,XPath 表达式只是表示查找所有具有包含www.domain.com 的href 属性的&lt;a&gt; 标记。

【讨论】:

  • 感谢您分享如何使用 DOMDocument 和 DOMXPath 方法。由于这是比正则表达式更好的解决方案,我最终选择了这条路线:)
猜你喜欢
  • 2018-07-06
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 2018-07-05
  • 2011-03-02
  • 2021-11-30
相关资源
最近更新 更多