Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get these values with XPath

Tags:

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>

OBJECTIVE

Need to get img src filename and get value of the input field which have name=title[]

WHAT I HAVE SO FAR

$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 contains just first image and not other

like image 412
RhymeGuy Avatar asked Jan 17 '26 17:01

RhymeGuy


1 Answers

Use the context parameter of DOMXPath::query() together with a relative query and check if img and input elements exist at all (it's not the case in your first table row):

$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>';
    }
}

This searches for the first <img> and <input name="title[]"> elements anywhere in each table row. If the table structure is always exacltly like shown above, you can optimize the script with a more explicit 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);

To get the attributes, iterate over $images and $inputs in parallel, for example with a 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>';
}
like image 134
Fabian Schmengler Avatar answered Jan 20 '26 09:01

Fabian Schmengler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!