选择值而不是选项+ XML DOM中的文本

时间:2014-01-15 13:55:16

标签: php symfony select xmldocument xmldom

我正在尝试获取名称= Reeks的选择列表的值,您可以在此页面上找到它:http://www.volleyvvb.be/?page_id=1083。选择列表如下所示:

<select class="vvb_tekst_middelkl" name="Reeks" onchange="this.form.submit();">
    <option value="%" selected="">ALLE REEKSEN</option>
    <option value="Liga A H">ETHIAS VOLLEY LEAGUE</option>
    <option value="Liga B H">LIGA B HEREN</option>
    <option value="Ere D">LIGA A DAMES</option>
    ...
</select>

这就是我获取选择列表的方式:

$html = file_get_contents("http://www.volleyvvb.be/?page_id=1083");

$crawler = new Crawler($html);

$crawler = $crawler->filter("select[name='Reeks']");
foreach ($crawler as $domElement) {
    foreach($domElement->childNodes as $child) {
        $value = $child->nodeValue;
        var_dump($value);
    }
}

我现在看到的是<option></option>之间的所有行ALLE REEKSEN, ETHIAS VOLLEY LEAGUE。但我也喜欢像Liga A H这样的价值观......我怎样才能选择它们?

2 个答案:

答案 0 :(得分:1)

以下代码

<?php
require_once("resources/simple_html_dom.php");
$html = file_get_contents("http://www.volleyvvb.be/?page_id=1083");
$doc = str_get_html($html);

$select = $doc->find("select[name='Reeks']");
foreach ($select as $domElement) {
    $child = $domElement->find('option');
    foreach($child as $option) {
        echo $option->getAttribute('value')."<br>";
    }
}
?>

给我你要求的输出。

Liga A H
Liga B H
Ere D
...

DomCrawler Component的等价物将是

$value = $child->nodeValue; // LIGA B HEREN, ...
$attribute = $child->attr('value'); // Liga B H, ...

有关详细信息,请查看Documentation here

答案 1 :(得分:0)

您需要使用each method for crawlerattr mehtod for crawler

试试这个:

$crawler->filter("select[name='Reeks']")->each(function ($node, $i) {
    echo $node->text();       -> // to print the value of html
    echo $node->attr('value'); -> // to print the value of value attrbuite
});
相关问题