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
Try:
$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
Outputs will be =>
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"
}
}
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>';
}