views:

594

answers:

2

Hay Guys,

i have a basic string with holds a html table. The table looks like this:

<TR>
    <TD>asdf, dfg</TD><TD>0915</TD><TD>0945</TD><TD></TD><TD>15</TD><TD>45</TD><TD></TD><TD>1315</TD>
</TR>
<TR>
    <TD>asdf, dfg</TD><TD>0915</TD><TD>0945</TD><TD></TD><TD>15</TD><TD>45</TD><TD></TD><TD>1315</TD>
</TR>
<TR>
    <TD>asdf, dfg</TD><TD>0915</TD><TD>0945</TD><TD></TD><TD>15</TD><TD>45</TD><TD></TD><TD>1315</TD>
</TR>
<TR>
    <TD>asdf, dfg</TD><TD>0915</TD><TD>0945</TD><TD></TD><TD>15</TD><TD>45</TD><TD></TD><TD>1315</TD>
</TR>

How would i use PHP to determine how many columns this table has?

Thanks

+2  A: 

As others have mentioned, if you have guaranteed valid HTML, and the table is guaranteed to have equal length rows, you can use simple string manipulation. Split the string on <tr>, then count the number of <td> in each piece:

function count_table_columns($html) {
    $html = strtolower($html);
    $rows = split('<tr>', $html);
    foreach($rows as $row) {
        if(!trim($row)) { continue; }
        return substr_count($row, '<td>');
    }
}

If there is the possibility of malformed HTML, use an HTML parser to parse the table, then iterate through the <tr> nodes, and count the subnodes of type <td>.

Here's one HTML parser to consider: http://sourceforge.net/projects/simplehtmldom/

Christian Oudard
+1  A: 

Count <TD>'s with substr_count(). If your string contains more than one row, then count <TR>'s and divide total number of <TD>'s by number of <TR>'s.

n1313
how about counting </td>'s? :P
Evernoob
First, author did not state that his html may be malformed. Second, to place string "<TD>" inside <td> tag one should replace brackets with corresponding entities, so they won't be caught up by substr_count().
n1313
@Evernoob: what for?
n1313
@Gorgapor - no it can't. If "<TD>" exists in the string, then it's HTML, and the browser will even attempt to render it that way. If it was text it would look like this "<TD>" which obviously wouldn't trigger a hit with substr_count(). If you did have an actual, errant "<TD>" then your count would be invalid only because the HTML is invalid, not because the algorithm isn't sound.
Peter Bailey
Agreed with Peter Bailey, and upvoted. It's much easier than Gorgapor's method, and Bailey's right about the entities.
Kawa
Yes, if your HTML is guaranteed valid, this is a good way to do it.
Christian Oudard
no, counting `<(/)td>`s will not work, becaue you can always have `colspan` attributes (you could count those too, then it’s probably correct)
knittl