I'm trying to sort out with regExps, so I met a problem on my way:
The thing is I have some random HTML file with plain text and only one table. Text can be before and after the table, table doesn't include <thead><tbody><tfoot> rowspan
and so on. So I need to split this table into several tables with 5 rows each and 5 or less the last one, with repeating first string of the original table in each table. So for example:
<table>
<tr>
<td>A</td><td>B</td>
</tr>
<tr>
<td>A1</td><td>B1</td>
</tr>
<tr>
<td>C</td><td>D</td>
</tr>
<tr>
<td>E</td><td>F</td>
</tr>
<tr>
<td>E1</td><td>F1</td>
</tr>
<tr>
<td>E2</td><td>F2</td>
</tr>
<tr>
<td>E3</td><td>F3</td>
</tr>
<tr>
<td>E4</td><td>F4</td>
</tr>
</table>
Should become:
<table>
<tr>
<td>A</td><td>B</td><--!!!(not needed to be in code)-->
</tr>
<tr>
<td>A1</td><td>B1</td>
</tr>
<tr>
<td>C</td><td>D</td>
</tr>
<tr>
<td>E</td><td>F</td>
</tr>
<tr>
<td>E1</td><td>F1</td>
</tr>
</table>
<table>
<tr>
<td>A</td><td>B</td><--!!!(not needed to be in code)-->
</tr>
<tr>
<td>E2</td><td>F2</td>
</tr>
<tr>
<td>E3</td><td>F3</td>
</tr>
<tr>
<td>E4</td><td>F4</td>
</tr>
</table>
This stuff I need to be done using PCRE in PHP including massives of templates and changes. So I have problems with realisation. For now I can find the first row like this <table>\s*?(<tr>(?:\s|.)*?<\/tr>)
and 4 going one-by-one rows (<tr>(?:\s|.)*?<\/tr>\s*){1,4}
but I cannot get how should I find all the occurrences of the second template so I can use them later on and how to stop searching if there is </table>
table closing tag. So please help
EDIT
question has been answered so the next level of it to add in original table tags <thead><tbody><tfoot>
. In output tables the structure of original table should be reconstructed, so I mean if the first row of original table was part of <thead>
tag it should be in <thead>
is all output tables.