1

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?

1
  • Do yuou want to count how many td are there inside one tr or inside all the table? Commented Sep 2, 2009 at 14:25

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

how about counting </td>'s? :P
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().
@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 "&lt;TD&gt;" 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.
Agreed with Peter Bailey, and upvoted. It's much easier than Gorgapor's method, and Bailey's right about the entities.
Yes, if your HTML is guaranteed valid, this is a good way to do it.
|
2

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/

Comments

0

This is simplehtmldom code that basically gets the number of columns in the HTML table

$table = $html->find('table',0);

foreach($table->find('tr') as $tr)
               {
                   $c=0;
                   foreach($tr->find('td') as $td) $c++;    
                   if( $c>$largest OR !isset($largest) ) $largest = $c; 
               }

$largest will give you column count.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.