Parser rows columns table html

2

I have to program a parser to save the columns and rows of an html table in the bd, this is my code:

$url = "https://xxxxxxxx";

        $html = file_get_contents($url);
        $html = fix($html);

        $fileName = fileSave("bienes", $html);

        $file = fopen($fileName, "r");

        $htmlTable = "";
        $switch = 0;
        $tipo = 0;

        while (($line_detail = fgets($file)) !== false) {
             .....
             .....
             // Aqui necesito recorrer las columas TD por fila TR para guardarlas en otro archivo, cómo puedo hacer eso más fácil ? 
        }
    
asked by Fabiola Cendales 26.05.2017 в 20:26
source

1 answer

1

I see that you capture the html, you save it in a local file, you iterate it and I suppose that there comes the table with the tr and td.

A simple way to do what you want is:

You extract the code from the table:

        $htmlTable = "";
        $switch = 0;
        $tipo = 0;

        while (($line_detail = fgets($file)) !== false) {


            $pos = strpos($line_detail, '<table');

            if ($pos !== FALSE) {
                /* Bien o Lote */
                $switch = 1;                                
            }

            if ($switch == 1) {
                $htmlTable .= $line_detail;
            }

            $pos = strpos($line_detail, '/table');
            if ($pos !== FALSE) {
                $switch = 0;                                
            }

        }

        $htmlTable = "<html>" . $htmlTable . "</html>";

Then, you iterate the rows TR and TD

$dom = new DOMDocument();
@$dom->loadHTML($htmlTable);
$rows = $dom->getElementsByTagName("tr");

$dataCols = "";
$identificador = "";

for ($i = 0; $i < $rows->length; $i++) {

    /* Solo los indices a procesar para el detalle de la subasta */
    /*if (!in_array($i, $detailRows)) {
        continue;
    }*/

    $cols = $rows->item($i)->getElementsbyTagName("td");
    for ($j=0; $j<$cols->length; $j++) {
         /* Aqui trabajas la logica segun lo que necesites ... */
    }

Finally you work the logic you need.

Completely solving what you need with the information you give is complicated. I hope this is your guide.

Greetings,

    
answered by 26.05.2017 / 20:36
source