0

Html what I am grabbing looks like this

<div id="table"><table>
<tr><td>Clubname</td><td>15</td><td>30</td></tr>
<tr><td>Clubname 2</td><td>15</td><td>30</td></tr>
<tr><td>Clubname 3</td><td>15</td><td>30</td></tr>
</table></div>

What i want is to find the tr where Clubname 2 is in and get the data from td[1] and td[2] and output this.

I want this done with simple_html_dom.php

What I already have is

require('simple_html_dom.php');
$html = file_get_html('webpage.html');
foreach($html->find('div#table') as $e)
echo $e->innertext . '<br>';

How to find the specific clubname and get the specific content from the td's from same tr?

=================================================================================

Okay thanks, what I did now is like you told me, only with variable, because later I want to use a variable.

<?php
    $clubname = 'Ajax';
    require('phpQuery/phpQuery.php');         
    $result = array();
    $limit = 2; //you need only the next two sibling

    $dom = phpQuery::newDocumentFile('http://soccer.gijsve.nl/test2.php');        
    $match = $dom->find('td:contains("'.$clubname.'")');        
    while( 0 < count( $match = $match->next() ) && ( 0 < $limit-- )  ){
            $result[] = $match->text();
    }
    var_dump($result);

?>

What I want now is to select the first td (td before the match) and the fourth and fifth for example. Because I need to know the scored goals, the points and the rank. See http://soccer.gijsve.nl/test2.php for the table I am grabbing.

4

2 回答 2

2

我向您推荐另一个简单的 DOM 工具: http ://code.google.com/p/phpquery/ 据我所知,它速度更快,并且选择器工作得更好。这个项目没有完成喷气机。但是dom阅读部分效果很好。选择器就像在 jQuery 中一样工作;)

<?php
    require('phpquery/phpQuery.php');         
    $result = array();
    $limit = 2; //you need only the next two sibling

    $dom = phpQuery::newDocumentFile('webpage.html');        
    $match = $dom->find('td:contains("Clubname 2")');        
    while( 0 < count( $match = $match->next() ) && ( 0 < $limit-- )  ){
            $result[] = $match->text();
    }
    var_dump($result);

    // other example:
    $match = $dom->find('td:contains("Clubname 2")');        
    $loses = $match->siblings('.loses')->text(); //matches the siblings of the $match element, and has loses class
    $wins = $match->siblings('.wins')->text(); //matches the siblings of the $match element, and has wins class

?>

这适用于您的示例 html,以及您评论的完整 html。

使用 simple_html_dom 您可以搜索文本,作为文本“标签”:

$textlist = $html->find('text');

这将返回所有文本块,您应该运行 foreach $textlist,如果元素的 html 等于您找到的内容,则将其保存到变量中,然后返回到它的父级(在完整的 html 中,第二个父)比到下一个兄弟,再到下一个兄弟,我认为比使用 phpQuery 更复杂。

于 2013-03-12T12:13:59.120 回答
1

Phpquery 也有兄弟选择器,让生活更轻松:

$dom->find('td:contains("Clubname 2") + td')->text(); # 15
$dom->find('td:contains("Clubname 2") + td + td')->text(); # 30
$dom->find('td:contains("Clubname 2") ~ td')->text(); # 15 30
于 2013-03-12T20:32:27.940 回答