0

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 \>";  
}

1 Answers1

0

Try using strip_tags() like this:

<?php
$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>';
$html = strip_tags($html);
echo $html;
?>

Here is a PHPFiddle of this function in action.

Output:

text a1 b1 text1 a2 b2 a3 b3 a4 b4 text2 a5 b5 a6 b7
Lil' Bits
  • 898
  • 2
  • 9
  • 24
  • is a html page with header and all a site need to function, i can`t use this, because i need to be able, first to identify the table(by id) and also if this tablel exist. I was thinking to give this example because i try to explain what i wanna do. maybe my question is not a good one to get my answer?! – user1972783 Jan 12 '13 at 18:29
  • and now i see it, the output is not what i want – user1972783 Jan 12 '13 at 18:44