3

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 只包含第一张图片而不是其他图片

4

2 回答 2

1

将 context 参数DOMXPath::query()与相对查询一起使用并检查 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>';
}
于 2013-02-03T23:05:53.103 回答
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输出将是 =>

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"
  }
}
于 2013-02-03T22:19:38.790 回答