1

我有一个脚本,它使用 cURL 从站点获取页面,我试图逐行剪切它,然后提取我需要的内容并删除该行。问题是,有时它会给我一个项目的正确位置,有时它不会取决于哪一行,它会交替出现。

我试图使用这些页面之一:http ://www.gametracker.com/search/

我使用的代码是:

while (strpos($contents, '<tr>'))
{
    $start = strpos($contents, '<tr>');
    $end = strpos($contents, '</tr>', $start) + 5;
    $rstring = substr($contents, $start, $end - $start);
    $contents = str_replace($rstring, '', $contents);
    //nameee
        $start = strpos($rstring, '[clantag]');
    $end = strpos($rstring, '</a>', $start);
    $name= trim(substr($rstring, $start, $end - $start));
    echo $name . '<br/>';
        //players
    $search = '<td class="c05 item_bgcolor_even">';
        $start = (strpos($rstring, $search) + strlen($search));
    $end = strpos($rstring, "</td>", $start);
    $players= trim(substr($rstring, $start, ($end - $start)));
    echo $players . '<br />';
    $map;
    $curPlayers;
    $maxPlayers;
    $ip;
}

它正确获取了服务器名称,但是当涉及到玩家时,一行我得到他们,另一行我得到 lor_odd">1230。

4

2 回答 2

1

您一直在为此类处理编写“错误”的代码。很难理解和维护,更不用说调试了。

如果您愿意放弃手动 HTML 解析并改用XPath查询,您的生活将变得更加轻松:

$doc = new DOMDocument();
$doc->loadHTML($contents); // $contents = the HTML that curl returns

$xpath = new DOMXPath($doc);

// You can now use as many XPath queries as you like; they look like this:
$nodes = $xpath->query(''); // you own query expression here
foreach ($nodes as $node) {
    $v = $node->nodeValue; // now do something with $v
}
于 2011-06-18T16:10:23.437 回答
1

使用DOM 解析器会更容易:

libxml_use_internal_errors(true);

$dom = new DOMDocument;
$dom->loadHTMLFile('http://www.gametracker.com/search/');
$xpath = new DOMXPath($dom);

// Get the rows from the search list
$rows = $xpath->query('//table[@class="table_lst table_lst_srs"]/tr');

for ($i=1; $i<$rows->length-1; $i++) {
    $row = $rows->item($i);

    // Get the columns for a row
    $cols = $row->getElementsByTagName('td');

    // Get the server name (3rd column)
    echo 'Name: '.trim($cols->item(2)->textContent).PHP_EOL;

    // Get the player count (5th column)
    echo 'Players: '.trim($cols->item(4)->textContent).PHP_EOL.PHP_EOL;
}

输出:

Name: NoobonicPlague.com | 24/7 RP, FastDL, Custom Mods
Players: 55/65

Name: RUS Coop-20 Semantic #1 (2.0.7.0)
Players: 20/20

Name: TAW.net -(Dallas)
Players: 32/32

Name: Dedicated
Players: 0/12

...
于 2011-06-18T16:23:38.473 回答