HTML colspan 0 alternative
My first answer is: refactor your code. If you need the total number of columns to build the table footer then the function you use to build the table body should return that number (and not only the HTML).
That said and only in case it's too complicated (or you don't have control about that code) you may simply count them by yourself, I would avoid any trick about colspan
because it's behavior isn't homogeneous (and it's not validated too).
You can easy count the number of cells using the first row (if the table is well formed all the rows have the same number of columns).
The first naive solution would be to split()
HTML tbody then to substr_count()
the <td/>
of the first row. Unfortunately this may work only in a very controlled situation (tables must be well formed, table may contain or not tbody
and it doesn't manage colspan
of that cells).
Better solution involves a small HTML parser (see this great post here on SO for a good and detailed list), when you have DOM then you can easily count TDs and check for their attributes (I say this in advance: no, you can't use regex to parse HTML).
To be honest I think refactoring is much more suitable...