[solved] regex issue; counting subpatterns..?
Posted: Sun Jun 13, 2004 5:36 pm
hey,
i've got a little problem on my hands here, perhaps one of you have a solution..
basically the scenario is as follows;
i have a number of csv files (variable row and column count), all of which have to be parsed and slapped into a standard html table for presentation. so far so good, no problems here.
the problem, however, is that not all fields have values, resulting in a massive bunch of empty table cells, which i'd like to get rid of - as well as set colspans for the remaining cells -with- values to fill out the space and fix the flow a bit..
long story short, i have this:
- and i need to get it into something like this:
now, i was thinking along the lines of using a preg_replace for this, searching for two or more empty cells, optionally one non-empty cell followed by one or more empty cells, and replacing them with a single cell with a colspan - however this would require counting subpatterns, i think, and i frankly dont have a clue on how to do that.. just recently started getting into regexes, and while they are definately extremely useful, they sure as hell cause a rather nasty headache..
so, anyone have an idea on how to do this? if i can simply solve this with a nice little regex that'd be preferable, but if someone has a radically different solution that'd work i'm all ears.

i've got a little problem on my hands here, perhaps one of you have a solution..
basically the scenario is as follows;
i have a number of csv files (variable row and column count), all of which have to be parsed and slapped into a standard html table for presentation. so far so good, no problems here.
the problem, however, is that not all fields have values, resulting in a massive bunch of empty table cells, which i'd like to get rid of - as well as set colspans for the remaining cells -with- values to fill out the space and fix the flow a bit..
long story short, i have this:
Code: Select all
<tr>
<td>something</td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td>something</td><td></td><td>meep</td>
</tr>
<tr>
<td></td><td></td><td></td><td>blah</td>
</tr>Code: Select all
<tr>
<td colspan='4'>something</td>
</tr>
<tr>
<td></td><td colspan='2'>something</td><td>meep</td>
</tr>
<tr>
<td colspan='3'></td><td>blah></td>
</tr>so, anyone have an idea on how to do this? if i can simply solve this with a nice little regex that'd be preferable, but if someone has a radically different solution that'd work i'm all ears.