0

当前代码是这样的:

include 'simple_html_dom.php';
    // Create DOM from URL or file
    $html = file_get_html('http://www.AnyLinkAlsoCan.com');


    // Find all links 
    foreach($html->find('a') as $element) 

           echo $element->href . '<br>';

它会抓取并找到这样的标签:

<a href="http://news.example.com/node">

并将输出它在网站上找到的所有链接。

例子

http://news.example.com.my/node/321072
http://news.example.com.my/taxonomy/term/2
http://news.example.com.my/node/321060?tid=2

我想搜索仅包含?tid=您在示例中的第三个 URL 上看到的 URL。

http://news.example.com.my/node/321060?tid=2

我替换echo $element->href="*?tid,但这只是返回错误。有人可以帮我弄这个吗?

4

2 回答 2

1

您可以使用 preg_match 或者您可以检查所有包含?tid的网址

<?php
include 'simple_html_dom.php';
// Create DOM from URL or file
$html = file_get_html('http://www.AnyLinkAlsoCan.com');


// Find all links 
foreach($html->find('a') as $element) {
       $search = '?tid';
       if(strpos($element->href,$search)) {
           echo $element->href . '<br>';
       }
}
?>
于 2013-09-04T13:52:08.020 回答
0

使用parse_url()解析每个 url,然后只选择你想要的基于PHP_URL_QUERY

于 2013-09-04T13:51:36.183 回答