如何使用XPath获取这些值

时间:2013-02-03 21:51:50

标签: php xpath

HTML

<table id='dg' border='0'  class="Table">
  <tr>
    <td class='text'>id.</td>
    <td class='text'>file</td>
    <td class='text'>alt</td>
  </tr>
  <tr>
    <td class='text'><input name='somename[]' type='hidden' value='1234'>
      1</td>
    <td class='text'><a href='#' onClick="javascript:openPopWindow('../../somefile.jpg');" ><img src='cms_thumb.php?imgsrc=somefile2.jpg' border='0' ></a></td>
    <td class='text'><input type='text' name='title[]' value='Value 1'></td>
  </tr>
  <tr>
    <td class='text'><input name='somename[]2' type='hidden' value='2345'>
      2</td>
    <td class='text'><a href='#' onClick="javascript:openPopWindow('../../somefile2.jpg');" ><img src='thumb.php?imgsrc=somefile2.jpg' border='0' ></a></td>
    <td class='text'><input type='text' name='title[]' value='Value 2'></td>
  </tr>
</table>

目的

需要获取img src文件名并获取name = title []

的输入字段的值

我有多远

$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);

foreach ($dom->getElementsByTagName('tr') as $node) { 
    $img = $xpath->query('//img')->item(0); 
    $img = str_replace("\'","",$img->getAttribute('src'));
    $img = str_replace("cms_thumb.php?imgsrc=","",$img);
    echo $img.'<br>';
}

$ img只包含第一张图片而非其他

2 个答案:

答案 0 :(得分:0)

尝试:

$html = <<<HTML
<table id='dg' border='0'  class="Table">
  <tr>
    <td class='text'>id.</td>
    <td class='text'>file</td>
    <td class='text'>alt</td>
  </tr>
  <tr>
    <td class='text'><input name='somename[]' type='hidden' value='1234'>
      1</td>
    <td class='text'><a href='#' onClick="javascript:openPopWindow('../../somefile.jpg');" ><img src='cms_thumb.php?imgsrc=somefile2.jpg' border='0' ></a></td>
    <td class='text'><input type='text' name='title[]' value='Value 1'></td>
  </tr>
  <tr>
    <td class='text'><input name='somename[]2' type='hidden' value='2345'>
      2</td>
    <td class='text'><a href='#' onClick="javascript:openPopWindow('../../somefile2.jpg');" ><img src='thumb.php?imgsrc=somefile2.jpg' border='0' ></a></td>
    <td class='text'><input type='text' name='title[]' value='Value 2'></td>
  </tr>
</table>
HTML;


$doc = new DOMDocument();
$doc->loadHTML($html);


foreach($doc->getElementsByTagName("td") as $td){
    foreach($td->getElementsByTagName("img") as $img){
        $arr_img[] = array(
            "img" => $img->getAttribute("src"),
        );
    }
    foreach($td->getElementsByTagName("input") as $name){
        if ($name->getAttribute("name")==="title[]"){
            $arr_value[] = array(
                "value" => $name->getAttribute("value")
            );
        }
    }
}

var_dump($arr_img); // In this array will be img src's
var_dump($arr_value); // In this array will be values of input elements which name equal to title[]

var_dump输出将为=&gt;

array(2) {
  [0]=>
  array(1) {
    ["img"]=>
    string(34) "cms_thumb.php?imgsrc=somefile2.jpg"
  }
  [1]=>
  array(1) {
    ["img"]=>
    string(30) "thumb.php?imgsrc=somefile2.jpg"
  }
}

array(2) {
  [0]=>
  array(1) {
    ["value"]=>
    string(7) "Value 1"
  }
  [1]=>
  array(1) {
    ["value"]=>
    string(7) "Value 2"
  }
}

答案 1 :(得分:0)

DOMXPath::query()的context参数与相对查询一起使用,并检查是否存在img和input元素(在第一个表行中不是这种情况):

$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);

foreach ($dom->getElementsByTagName('tr') as $node) { 
    $img = $xpath->query('.//img', $node)->item(0);
    $input = $xpath->query('.//input[@name="title[]"]', $node)->item(0);
    if ($img && $input) {
        echo $img->getAttribute('src'), ' - ';
        echo $input->getAttribute('value'), '<br>';
    }
}

这会在每个表格行的任意位置搜索第一个<img><input name="title[]">元素。如果表结构总是如上所示,您可以使用更明确的XPath优化脚本:

$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);

$table = $dom->getElementById('dg');
$images = $xpath->query('tr/td/a/img', $table);
$inputs = $xpath->query('tr/td/input[@name="title[]"]', $table);

要获取属性,请并行迭代$images$inputs,例如使用MultipleIterator

$iterator = new MultipleIterator();
$iterator->attachIterator(new IteratorIterator($images));
$iterator->attachIterator(new IteratorIterator($inputs));
foreach ($iterator as $items) {
    $src = $items[0]->getAttribute('src');
    $value = $items[1]->getAttribute('value');
    echo $src, ' - ', $value, '<br>';
}