I use curl to get a HTML file and I'm trying to use the DOM to get what I need from it.
I have tried everything (from my point of view), but it's not working like I want.
Let's say I have this:
html = '<table cellpadding="0" cellspacing="0" id="uniq">
<tr>
<th>text</th>
<td class="1_th">a1</td>
<td class="1_td">b1</td>
</tr>
<tr>
<th rowspan="3">text1</th>
<td class="1_th">a2</td>
<td class="1_td">b2</td>
</tr>
<tr>
<td class="1_th">a3</td>
<td class="1_td">b3</td>
</tr>
<tr>
<td class="1_th">a4</td>
<td class="1_td">b4</td>
</tr>
<tr>
<th rowspan="2">text2</th>
<td class="1_th">a5</td>
<td class="1_td">b5</td>
</tr>
<tr>
<td class="1_th">a6</td>
<td class="1_td">b7</td>
</tr>
</table>'
I want to be able to echo this with PHP:
text - a1 -b1
text1 - a2 -b2
text1 -a3 -b3
text1 -a4 -b4
text2 -a5 -b5
text2 -a6 -b6
The table is large, and th
have variable rowspans between 15 and 20. I want to echo
because I want to insert these values in MySQL.
I tried this:
$dom = new DOMDocument();
@$dom->loadHTML($html);
$x = new DOMXPath($dom);
$table = $x->query('//*[@id="uniq"]')->item(0);
$rows = $table->getElementsByTagName("tr");
foreach ($rows as $row) {
$tds = $row->nodeValue;
echo $th;
}
Nevermind, i found solution for what i need, thanks for trying to help me
This is i make and is ok for me:
$dom = new DOMDocument();
@$dom->loadHTML($html);
$x = new DOMXPath($dom);
$table = $x->query("//*[@id='item_specification']/tr");
$rows = $table;
foreach ($rows as $row) {
$atr_name = $row -> getElementsByTagName('td')->item(0)->nodeValue;
$atr_val = $row -> getElementsByTagName('td')->item(1)->nodeValue;
$cell1 = $row -> getElementsByTagName('th');
`$ifth = $cell1->length;
`if ($ifth == 1) {
$atr_cat = $row->getElementsByTagName('th')->item(0)->nodeValue;
}
echo "{$atr_cat} - {$atr_name} - {$atr_val} <br \>";
}