English 中文(简体)
DIV内所有链接的查找和印刷
原标题:Finding and Printing all Links within a DIV

我正试图在四处找到所有链接,然后印刷这些链接。

我正在使用简单的“超文本”来复制“超文本”文件。 在这方面,我要宣读直线评论,让我知道我在哪里错了。

include( simple_html_dom.php );  

$html = file_get_html( tester.html );

$articles = array();

//find the div the div with the id abcde
foreach($html->find( #abcde ) as $article) {

    //find all a tags that have a href in the div abcde
    foreach($article->find( a[href] ) as $link){

        //if the href contains singer then echo this link
        if(strstr($link,  singer )){

            echo $link;

        }

    }

}

目前发生的情况是,上述装备需要很长时间才能装满(无论何时必须填满)。 我印刷了它在每一处所做的事情,因为等待时间太长,我发现,它通过事情我不需要! 这表明我的法典是错误的。

沉默基本上就是这样:

<div id="abcde">
<!-- lots of html elements -->
<!-- lots of a tags -->
<a href="singer/tom" />
<img src="image..jpg" />
</a>
</div>

感谢一切帮助

最佳回答

采用该软件的ID选择四(或以任何方式)的正确方法是:

$html->find( div[id=abcde] );

此外,由于国际身份识别器应当是独一无二的,因此应当做到以下几点:

//find all a tags that have a href in the div abcde
$article = $html->find( div[id=abcde] , 0);

foreach($article->find( a[href] ) as $link){

    //if the href contains singer then echo this link
    if(strstr($link,  singer )){
        echo $link;
    }
}
问题回答

为什么不要让你利用在多瑙河上的延伸?

<?php

$cont = file_get_contents("http://stackoverflow.com/") or die("1");

$doc = new DOMDocument();
@$doc->loadHTML($cont) or die("2");

$nodes = $doc->getElementsByTagName("a");

for ($i = 0; $i < $nodes->length; $i++) {
    $el = $nodes->item($i);
    if ($el->hasAttribute("href"))
        echo "- {$el->getAttribute("href")}
";
}

专 员

... (lots of links before) ...
- http://careers.stackoverflow.com
- http://serverfault.com
- http://superuser.com
- http://meta.stackoverflow.com
- http://www.howtogeek.com
- http://doctype.com
- http://creativecommons.org/licenses/by-sa/2.5/
- http://www.peakinternet.com/business/hosting/colocation-dedicated#
- http://creativecommons.org/licenses/by-sa/2.5/
- http://blog.stackoverflow.com/2009/06/attribution-required/




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签