PHP/HTML: Replace everything between <TD colspan=6 ...> .... </TD> - php

I want to replace everything between a TD-Tag that has the attribute/value colspan=6.
All this I want to replace ...
<TD colspan=6 rowspan=4 align="center" nowrap="1">
<TABLE>
<TR>
<TD width="50%" nowrap=1><font size="3" face="Arial">
Some Text
</font>
</TD>
</TR>
<TR>
<TD width="50%" nowrap=1><font size="3" face="Arial">
Some Text
</font>
</TD>
</TR>
<TR>
<TD width="50%" nowrap=1><font size="3" face="Arial">
Some Text
</font>
</TD>
</TR>
</TABLE>
</TD>
...with this lines:
<TD colspan=12 rowspan=2 align="center" nowrap="1">
<TABLE>
<TR>
<TD>frei</TD>
Some Text
</TR>
</TABLE>
</TD>
Any Ideas? Maybe with simple_html_dom.php?

Solved with PHP and Regex:
$plan1 = preg_replace('~<TD colspan=6.*?</TR></TABLE></TD>~s",
"<TD colspan=12 rowspan=2 align="center" nowrap="1">
<TABLE><TR><TD></TD></TR></TABLE></TD>', $plan1);

Related

Get only the table rows from the first table - Simple HTML Dom (PHP)

<table border="3" rules="all" cellpadding="1" cellspacing="1">
<tbody>
<tr>
<td align="center">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial" color="#000000"> <b>Maandag</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>Dinsdag</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>Woensdag</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>Donderdag</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>Vrijdag</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>1</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="18" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *verlof </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_4. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O114 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>2</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_3. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O114 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_7. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4A </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>3</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_1. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_1. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>4</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>5</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> stip </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> Os1 </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_5. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V5B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V5C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_5. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V5B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V5C </font>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>6</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_3. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_3. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>7</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_4. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_7. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4A </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_4. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_7. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4A </font>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>8</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_5. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V5B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V5C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_1. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>9</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
</tbody>
</table>
My school supplies me with the following timetable (as seen above), but when I want to parse the table using simplehtmldom. From this table I want to collect the data so that I can use it in my App.
What query in simplehtmldom can i use to select only the first table rows?
Like this: Table->(all rows)
When I try this query: echo($html->find('table tr',0)); And I change the the zero to 2 (for example), it doesn't return the next table row of the main table. But Instead it returns the frist table row of sub table in the first table row.
Not that I can't change the way the table is presented, this is the table I have to work with.
What I get now when I execute: echo($html->find('table tr',2)); -> "Maandag"
What I would like to get: <tr><td rowspan="2" align="center" nowrap="1"><table><tbody><tr><td align="center" nowrap="1"><font size="2" face="Arial"> <b>2</b> </font> </td> </tr></tbody></table></td> <td colspan="12" rowspan="2" align="center" nowrap="1"><table><tbody><tr><td></td></tr></tbody></table></td> <td colspan="12" rowspan="2" align="center" nowrap="1"><table><tbody><tr><td width="33%" nowrap="1"><font size="2" face="Arial"> *in_3. </font> </td> <td width="33%" nowrap="1"><font size="2" face="Arial"> O114 </font> </td> <td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font> </td> </tr><tr><td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4C </font> </td> </tr></tbody></table></td> <td colspan="12" rowspan="2" align="center" nowrap="1"><table><tbody><tr><td width="33%" nowrap="1"><font size="2" face="Arial"> *in_7. </font> </td> <td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font> </td> <td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font> </td> </tr><tr><td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4A </font> </td> </tr></tbody></table></td> <td colspan="12" rowspan="2" align="center" nowrap="1"><table><tbody><tr><td></td></tr></tbody></table></td></tr> (the next tablerow of the first (main) table)
The way I load in the html table:
$html = file_get_html($timetableURL);
$timeTable = $html->find('table',0); //I need to do this because the page with the timetable has more html tables, I tested this and it always fetches the timetable as seen above.
A related question: PHP Simple HTML DOM Parser how to get TR only from first table
Only I want to select the second or third row of the first table for example.
echo($timeTable->find('tr',2));
It's a little bit convoluted with simple-html-dom:
// find the first table
$table = $html->find('table', 0);
// iterate all tr's
foreach($table->find('tr') as $tr){
// skip the tr's that are not top level
if($tr->parent->parent != $table) continue;
do_something_with($tr);
}
I used a while loop and the simple-html-dom nextSibling() function.
$tableCell = $row->find('td', 0);
while($tableCell){
$tableCell = $tableCell->next_sibling(); //Go to the next tableCell
}
You can use jQuery selector :first to fetch the first element from the DOM
You can do it like this:
$(function() {
var table = $('table:first');
var table_rows = table.find('tr');
alert(" There are " + table_rows.length + " rows in Table");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="3" rules="all" cellpadding="1" cellspacing="1">
<tbody>
<tr>
<td align="center">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial" color="#000000"> <b>Maandag</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>Dinsdag</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>Woensdag</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>Donderdag</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>Vrijdag</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>1</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="18" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *verlof </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_4. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O114 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>2</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_3. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O114 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_7. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4A </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>3</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_1. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_1. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>4</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>5</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> stip </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> Os1 </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_5. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V5B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V5C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_5. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V5B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V5C </font>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>6</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_3. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_3. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>7</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_4. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_7. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4A </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_4. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_7. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V4B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V4A </font>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>8</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_5. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V5B </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V5C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td width="33%" nowrap="1"><font size="2" face="Arial"> *in_1. </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> O112 </font>
</td>
<td width="33%" nowrap="1"><font size="2" face="Arial"> V6A </font>
</td>
</tr>
<tr>
<td colspan="3" width="33%" nowrap="1"><font size="2" face="Arial"> V6C </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td align="center" nowrap="1"><font size="2" face="Arial"> <b>9</b> </font>
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
<td colspan="12" rowspan="2" align="center" nowrap="1">
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
</tbody>
</table>
Hope this helps!
Traversing your DOM Structure, and I find that the tr that you want is the 4th child tr, which directly under a tbody element under the table.
So, the query you need might be something like this
// assume table is the root/parent DOM
$tr = $table->find('tbody > tr:nth-child(4)');
var_dump($tr->html());
Sorry if the php syntax is incorrect. That is actually tested against phpquery library.

Python regex ignore new line

I have web page look like this
<td valign="top">
<table width="100%" border="0" cellspacing="2" cellpadding="1" class="main_tb3">
<tr>
<td colspan="2">
<div align="center">
<a href="/title/name.php" target="_blank">
<img src="./movie/image.jpg" alt="TitleName" border="0" height="100" width="225" />
</a>
</div>
</td>
</tr>
<tr>
<td colspan="2"><h1 align="center">Title - secondname</h1></td>
</tr>
<tr>
<td><span class="style10">Cat1 :</span></td>
<td>1st name</td>
</tr>
<tr>
<td width="32%"><span class="style10">Cat2 :</span></td>
<td width="68%"><b><i>secondname</i></b></td>
</tr>
<tr>
<td><span class="style10">cat4 :</span></td>
<td>Bla bla</td>
</tr>
<tr>
<td><span class="style10">Cat3 :</span></td>
<td>thirdName2</td>
</tr>
</table>
</td>
<td valign="top">
<table width="100%" border="0" cellspacing="2" cellpadding="1" class="main_tb3">
<tr>
<td colspan="2">
<div align="center">
<a href="/title/name.php" target="_blank">
<img src="./movie/image.jpg" alt="TitleName" border="0" height="100" width="225" />
</a>
</div>
</td>
</tr>
<tr>
<td colspan="2"><h1 align="center">Title - secondname</h1></td>
</tr>
<tr>
<td><span class="style10">Cat1 :</span></td>
<td>1st name</td>
</tr>
<tr>
<td width="32%"><span class="style10">Cat2 :</span></td>
<td width="68%"><b><i>secondname</i></b></td>
</tr>
<tr>
<td><span class="style10">cat4 :</span></td>
<td>Bla bla</td>
</tr>
<tr>
<td><span class="style10">Cat3 :</span></td>
<td>thirdName2</td>
</tr>
</table>
</td>
I would like to get certain values from this site using python regex.
After <div align="center"> I like to get href value: "/title/name.php" and img src: "./movie/image.jpg" and Title - secondname from <h1 align="center">Title - secondname</h1>
i have tried this:
regex = 'class="main_tb3"*\n<a href="(.+?)" target="_blank">\n<img src="(.+?)"'
please help me
you can use below regex
For href value: <a href="(.*?)"
For Image src: <img src="(.*?)"
For Title: titleid=12">(.*?)<
You will find it a lot simpler to install something like BeautifulSoup to do this:
from bs4 import BeautifulSoup
html = """
<td valign="top">
<table width="100%" border="0" cellspacing="2" cellpadding="1" class="main_tb3">
<tr>
<td colspan="2">
<div align="center">
<a href="/title/name.php" target="_blank">
<img src="./movie/image.jpg" alt="TitleName" border="0" height="100" width="225" />
</a>
</div>
</td>
</tr>
<tr>
<td colspan="2"><h1 align="center">Title - secondname</h1></td>
</tr>
<tr>
<td><span class="style10">Cat1 :</span></td>
<td>1st name</td>
</tr>
<tr>
<td width="32%"><span class="style10">Cat2 :</span></td>
<td width="68%"><b><i>secondname</i></b></td>
</tr>
<tr>
<td><span class="style10">cat4 :</span></td>
<td>Bla bla</td>
</tr>
<tr>
<td><span class="style10">Cat3 :</span></td>
<td>thirdName2</td>
</tr>
</table>
</td>
<td valign="top">
<table width="100%" border="0" cellspacing="2" cellpadding="1" class="main_tb3">
<tr>
<td colspan="2">
<div align="center">
<a href="/title/name.php" target="_blank">
<img src="./movie/image.jpg" alt="TitleName" border="0" height="100" width="225" />
</a>
</div>
</td>
</tr>
<tr>
<td colspan="2"><h1 align="center">Title - secondname</h1></td>
</tr>
<tr>
<td><span class="style10">Cat1 :</span></td>
<td>1st name</td>
</tr>
<tr>
<td width="32%"><span class="style10">Cat2 :</span></td>
<td width="68%"><b><i>secondname</i></b></td>
</tr>
<tr>
<td><span class="style10">cat4 :</span></td>
<td>Bla bla</td>
</tr>
<tr>
<td><span class="style10">Cat3 :</span></td>
<td>thirdName2</td>
</tr>
</table>
</td>"""
soup = BeautifulSoup(html)
for table in soup.find_all("table", class_="main_tb3"):
print table.find('a').get('href')
print table.find('h1').text
For the HTML you have given, this will print the following:
/title/name.php
Title - secondname
/title/name.php
Title - secondname

Getting tags in DOMDocument

I'm trying to get the HTML markup of a table in a page:
$new_dom = new DOMDocument();
$table = '';
$nodesTable = $this->dom->getElementsbyTagName("table");
foreach($nodesTable as $nodeTable){
$color = $nodeTable->getAttribute('bordercolordark');
if ($color == '#73BAFF') {
$table = $nodeTable;
}
}
$new_dom->appendChild($table);
echo $new_dom->saveHTML();
Here is somepage.html:
<html>
<table>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
</table>
<table border="1" cellpadding="0" width="500" bordercolorlight="#ACD6FF" bordercolordark="#73BAFF" align="center">
<tr>
<td rowspan="2" colspan="2" bgcolor="#73BAFF"> </td>
<td colspan="3" align="center" bgcolor="#ACD6FF"> Element 1 </td>
<td colspan="3" align="center" bgcolor="#ACD6FF"> Element 2 </td>
</tr>
<tr>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
<td width="50" align="center" bgcolor="#ACD6FF"> 50 </td>
</tr>
<tr>
<td bgcolor="#ACD6FF" width="155" align="center"> Row 1</td>
<td bgcolor="#ACD6FF" width="45" align="center"> 30 </td>
<td align="center"> 50 </td>
<td align="center"> 50 </td>
<td align="center"> 50 </td>
<td align="center"> 50 </td>
<td align="center"> 50 </td>
<td align="center"> 50 </td>
</tr>
<tr>
<td bgcolor="#ACD6FF" width="155" align="center"> Row 2</td>
<td bgcolor="#ACD6FF" width="45" align="center"> 30 </td>
<td align="center"> 60 </td>
<td align="center"> 60 </td>
<td align="center"> 60 </td>
<td align="center"> 60 </td>
<td align="center"> 60 </td>
<td align="center"> 60 </td>
</tr>
<tr>
<td bgcolor="#ACD6FF" width="155" align="center"> Row 3</td>
<td bgcolor="#ACD6FF" width="45" align="center"> 30 </td>
<td align="center"> 70 </td>
<td align="center"> 70 </td>
<td align="center"> 70 </td>
<td align="center"> 70 </td>
<td align="center"> 70 </td>
<td align="center"> 70 </td>
</tr>
</table>
<table>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
</table>
<table>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
<tr> <td> 10 </td> </tr>
</table>
</html>
$new_dom just outputs \n instead of HTML markup. I tried looking at this answer: PHP DOMDocument stripping HTML tags, but appending the table this way didn't work either.
Fatal error: Uncaught exception 'DOMException' with message 'Wrong Document Error'
So you cannot move nodes from one document to another... If you want to do that, you have to use importNode() with the deep flag.
$dom = new DOMDocument();
$dom->loadHTMLFile('x.html');
$new_dom = new DOMDocument();
$table = '';
$nodesTable = $dom->getElementsbyTagName("table");
foreach($nodesTable as $nodeTable){
$color = $nodeTable->getAttribute('bordercolordark');
if ($color == '#73BAFF') {
$table = $new_dom->importNode($nodeTable, true);
}
}
$new_dom->appendChild($table);
echo $new_dom->saveHTML();
This imports only the table element, but not the children...
note: I'd disable the entity loader in your case libxml_disable_entity_loader(true);. I am not sure whether XEE attacks work with loadHTML() too, but just for the sake of security.

how to use sql query result in html table

I am trying to create a html table showing results from php sql query. it is a result page of students php code is as under
$r1=$_GET["r"];
$con=mysqli_connect(localhost,chumspai_tlss,Tls121,chumspai_tlsResult);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM nursery_blue_ WHERE sr_='$r1'");
while($row = mysqli_fetch_array($result))
{
html code is
<pre>
<form name="frmResult" id="frmResult" action="" method="post" onsubmit="return checkEmpty();">
<table width="80%" cellpadding="5" cellspacing="5" border="0">
<tr>
<td class="heading noborder">Enter Your Roll Number:</td>
<td class="noborder"><input type="text" id="r" name="r" value="" /></td>
</tr>
<tr>
<!--
<td class="heading noborder">Enter Your Name:</td>
<td class="noborder"><input type="text" id="name" name="name" value="" /></td>
</tr>
<tr>
<td class="heading noborder">Search by</td>
<td class="noborder"><input type="radio" id="option" name="option" value="rno" checked="checked" />
Roll No
<input type="radio" id="option" name="option" value="name" />
Name </td>
</tr>
-->
<tr>
<td class="noborder"> </td>
<td class="noborder"><input type="submit" name="submit" value="Search" />
<input type="reset" name="reset" value="Clear" />
</td>
</tr>
<!--<tr>
<td colspan="2"> <embed src="images/wait.swf"></embed></td>
</tr> -->
</table>
</form>
<div style="border:1px solid #000000;">
<table width="100%" cellpadding="10" cellspacing="0" border="0">
<tr>
<td class="heading grey" width="30%">RNO</td>
<td><?php
Print $row['sr_'];
?>
</td>
</tr>
<tr>
<td class="heading grey">NAME</td>
<td class="shade"></td>
</tr>
<tr>
<td class="heading grey">FATHER</td>
<td></td>
</tr>
<tr>
<td class="heading grey">regno</td>
<td></td>
</tr>
</table>
<table width="100%" cellpadding="10" cellspacing="0" border="0">
<tr class="grey">
<td rowspan="2" class="heading">Sr.no </td>
<td rowspan="2" class="heading">Name of subject </td>
<td rowspan="2" class="heading">Maximum Marks</td>
<td colspan="7" class="heading">detail of marks Obtained</td>
<tr class="grey">
<td class="heading">PART ONE</td>
<td class="heading">Total</td>
</tr>
<tr>
<td>1</td>
<td>Urdu</td>
<td></td>
<td> </td>
<td></td>
</tr>
<tr class="shade">
<td>2</td>
<td>English</td>
<td></td>
<td> </td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>Islamyat</td>
<td></td>
<td> </td>
<td></td>
</tr>
<tr class="shade">
<td>4</td>
<td>pakstudies</td>
<td></td>
<td> </td>
<td></td>
</tr>
<tr class="shade">
<td>6</td>
<td></td>
<td></td>
<td></td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td></td>
<td></td>
<td></td>
<td>0</td>
</tr>
<tr class="shade">
<td>8</td>
<td></td>
<td></td>
<td></td>
<td>0</td>
</tr>
<tr class="shade">
<td>9</td>
<td></td>
<td></td>
<td></td>
<td>0</td>
</tr>
<tr class="grey">
<td colspan="2" class="heading">TOTAL</td>
<td class="heading">1100</td>
<td colspan="4" class="heading"></td>
</tr>
<tr class="grey">
<td colspan="3" class="heading">NOTIFICATION</td>
<td class="heading"></td>
<td class="heading"></td>
<td colspan="2" class="heading"></td>
</tr>
<tr>
<td colspan="7">(i) This provisional result intimation is issued as a notice only. Errors and omissions are excepted.</td>
</tr>
</table>
</pre>
please help me how to embed this php query with this html table and html form also.
you are not so far.
The variable $row is an array containing your data. Try this to see it's structure in your while call:
print_r($row);
Using this command you will see the name of each item of your array. Note it somewhere. Then you can do something like this:
...<td><?php echo $row['desired_column_name']; ?></td>...
If you receive data from your mysql query, this should do the trick.
Hope it helps,
Paul
Try This :
$result = mysql_query("select * from emp");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td id=SrNo$cnt >".$row['eno']."</td>";
echo "<td id=ItemId$cnt >".$row['eId']."</td>";
echo "<td>". "<button name='Update' id='update' onclick='show(".$cnt.")'>UPDATE</button>"."</td>";
echo "<td>". "<button name='Report' id='show' onclick='Report(".$row['SrNo'].")'>REPORT</button>"."</td>";
echo "</tr>";
echo "<div id=show$cnt>";
echo "</div>";
$cnt++;
}

How do I extract values from a html page stored as string using curl function

I am using PHP / curl to get a HTML into a string and then i need to extract the following data and then project a graph out of it .
The data I want looks like :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Linux (vers 25 March 2009), see www.w3.org" />
<title></title>
</head>
<body>
<table>
<tbody>
<tr>
<td>
<h3>Income</h3>
</td>
</tr>
<tr>
<td>Operating income</td>
<td class="numericalColumn">22,922.00</td>
<td class="numericalColumn">21,507.30</td>
<td class="numericalColumn">17,492.60</td>
<td class="numericalColumn">13,683.90</td>
<td class="numericalColumn">10,227.12</td>
</tr>
<tr>
<td>
<h3>Expenses</h3>
</td>
</tr>
<tr>
<td>Material consumed</td>
<td class="numericalColumn">4,029.40</td>
<td class="numericalColumn">3,442.60</td>
<td class="numericalColumn">2,952.30</td>
<td class="numericalColumn">1,889.00</td>
<td class="numericalColumn">1,367.67</td>
</tr>
<tr>
<td>Manufacturing expenses </td>
<td class="numericalColumn">2,213.20</td>
<td class="numericalColumn">1,841.80</td>
<td class="numericalColumn">299.80</td>
<td class="numericalColumn">120.50</td>
<td class="numericalColumn">1,020.70</td>
</tr>
<tr>
<td>Personnel expenses</td>
<td class="numericalColumn">9,062.80</td>
<td class="numericalColumn">9,249.80</td>
<td class="numericalColumn">7,409.10</td>
<td class="numericalColumn">5,768.20</td>
<td class="numericalColumn">4,279.03</td>
</tr>
<tr>
<td>Selling expenses</td>
<td class="numericalColumn">378.10</td>
<td class="numericalColumn">308.40</td>
<td class="numericalColumn">532.10</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">171.05</td>
</tr>
<tr>
<td>Adminstrative expenses</td>
<td class="numericalColumn">1,737.00</td>
<td class="numericalColumn">1,906.00</td>
<td class="numericalColumn">2,583.70</td>
<td class="numericalColumn">2,651.70</td>
<td class="numericalColumn">904.78</td>
</tr>
<tr>
<td>Expenses capitalised</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
</tr>
<tr>
<td>Cost of sales</td>
<td class="numericalColumn">17,420.50</td>
<td class="numericalColumn">16,748.60</td>
<td class="numericalColumn">13,777.00</td>
<td class="numericalColumn">10,429.40</td>
<td class="numericalColumn">7,743.22</td>
</tr>
<tr>
<td>Operating profit</td>
<td class="numericalColumn">5,501.50</td>
<td class="numericalColumn">4,758.70</td>
<td class="numericalColumn">3,715.60</td>
<td class="numericalColumn">3,254.50</td>
<td class="numericalColumn">2,483.90</td>
</tr>
<tr>
<td>Other recurring income</td>
<td class="numericalColumn">434.20</td>
<td class="numericalColumn">468.20</td>
<td class="numericalColumn">326.90</td>
<td class="numericalColumn">288.70</td>
<td class="numericalColumn">113.59</td>
</tr>
<tr>
<td>Adjusted PBDIT</td>
<td class="numericalColumn">5,935.70</td>
<td class="numericalColumn">5,226.90</td>
<td class="numericalColumn">4,042.50</td>
<td class="numericalColumn">3,543.20</td>
<td class="numericalColumn">2,597.49</td>
</tr>
<tr>
<td>Financial expenses</td>
<td class="numericalColumn">108.40</td>
<td class="numericalColumn">196.80</td>
<td class="numericalColumn">116.80</td>
<td class="numericalColumn">7.20</td>
<td class="numericalColumn">3.13</td>
</tr>
<tr>
<td>Depreciation </td>
<td class="numericalColumn">579.60</td>
<td class="numericalColumn">533.60</td>
<td class="numericalColumn">456.00</td>
<td class="numericalColumn">359.80</td>
<td class="numericalColumn">292.26</td>
</tr>
<tr>
<td>Other write offs</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
</tr>
<tr>
<td>Adjusted PBT</td>
<td class="numericalColumn">5,247.70</td>
<td class="numericalColumn">4,496.50</td>
<td class="numericalColumn">3,469.70</td>
<td class="numericalColumn">3,176.20</td>
<td class="numericalColumn">2,302.10</td>
</tr>
<tr>
<td>Tax charges </td>
<td class="numericalColumn">790.80</td>
<td class="numericalColumn">574.10</td>
<td class="numericalColumn">406.40</td>
<td class="numericalColumn">334.10</td>
<td class="numericalColumn">286.10</td>
</tr>
<tr>
<td>Adjusted PAT</td>
<td class="numericalColumn">4,456.90</td>
<td class="numericalColumn">3,922.40</td>
<td class="numericalColumn">3,063.30</td>
<td class="numericalColumn">2,842.10</td>
<td class="numericalColumn">2,016.00</td>
</tr>
<tr>
<td>Non recurring items</td>
<td class="numericalColumn">441.10</td>
<td class="numericalColumn">-948.60</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">38.33</td>
</tr>
<tr>
<td>Other non cash adjustments</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-33.85</td>
</tr>
<tr>
<td>Reported net profit</td>
<td class="numericalColumn">4,898.00</td>
<td class="numericalColumn">2,973.80</td>
<td class="numericalColumn">3,063.30</td>
<td class="numericalColumn">2,842.10</td>
<td class="numericalColumn">2,020.48</td>
</tr>
<tr>
<td>Earnigs before appropriation</td>
<td class="numericalColumn">4,898.00</td>
<td class="numericalColumn">2,973.80</td>
<td class="numericalColumn">3,063.30</td>
<td class="numericalColumn">2,842.10</td>
<td class="numericalColumn">2,020.48</td>
</tr>
<tr>
<td>Equity dividend</td>
<td class="numericalColumn">880.90</td>
<td class="numericalColumn">586.00</td>
<td class="numericalColumn">876.50</td>
<td class="numericalColumn">873.70</td>
<td class="numericalColumn">712.88</td>
</tr>
<tr>
<td>Preference dividend</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
<td class="numericalColumn">-</td>
</tr>
<tr>
<td>Dividend tax</td>
<td class="numericalColumn">128.30</td>
<td class="numericalColumn">99.60</td>
<td class="numericalColumn">148.90</td>
<td class="numericalColumn">126.80</td>
<td class="numericalColumn">99.98</td>
</tr>
<tr>
<td>Retained earnings</td>
<td class="numericalColumn">3,888.80</td>
<td class="numericalColumn">2,288.20</td>
<td class="numericalColumn">2,037.90</td>
<td class="numericalColumn">1,841.60</td>
<td class="numericalColumn">1,207.62</td>
</tr>
</tbody>
</table>
</body>
</html>
I want to extract each value like Manufacturing Data and the values of all the years mentioned in that line. How do I go about this?
I found something like preg_match('#<tr><th>(.*)</th> <td><b>price</b></td></tr>#', $content, $match); but that doesn't get the values I want.
If i understood you question well you want something like this to be done. this was written by me so if you need clarifications i'd love to help.
cheers !
You can use libraries like PHP Simple HTML DOM Parser to extract data from HTML/XHTML.
http://simplehtmldom.sourceforge.net/manual.htm
An example:
$pageDom = str_get_html( $rawHtmlData );
foreach( $pageDom->find( 'td' ) as $tblElem )
{
if( FALSE !== stristr( $tblElem->innertext, 'Manufacturing expenses' ) )
{
// Do stuff
}
}

Categories