How to get these values using 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>
TARGET
You need to get the name of the img src file and get the value of the input field, which has name = name []
WHAT I AM 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 only the first image, not the other
source to share
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"
}
}
source to share
Use context parameter DOMXPath::query()
along with relative query and check if img and input elements exist at all (they don't) in the first row of the table):
$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>';
}
}
It searches for the first elements <img>
and <input name="title[]">
in any row of the table. If the table structure is always explicitly higher as shown above, you can optimize the script with 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 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>';
}
source to share