在字符串中有一个表,该字符串可以包含此字符串($table_data):
<table>
<tr class="rowset-1"><td>cell1</td></tr>
<tr class="rowset-2"><td>cell1</td></tr>
<tr class="rowset-3"><td>cell1</td></tr>
</table>从这个字符串中,我想要计算表中有多少行。我确实数过这样的行数:
$i = 1;
while( true )
{
if (strpos( $table_data, 'rowset-'.$i ) === false )
{
$nr_rows = $i - 1;
break;
}
$i++;
}我所做的只是查找类行集-x是一个以1开头的数字。如果它不存在,我已经计算了行数……(在以上情况下为3行)
我在寻找一种获得最多行集的方法-?一串(在哪里?)是使用更好的方法()的最大数目(我的直觉是,我目前的方法绝对不是最好的)。
发布于 2021-01-13 22:01:37
有时候你需要告诉这个世界你是多么的愚蠢,直到你意识到你应该做些什么;-)
我意识到我可以用substr_count来实现我想要的。
$table_data = '
<table>
<tr class="rowset-1"><td class="colset-1">cell1</td><td class="colset-2">cell1</td></tr>
<tr class="rowset-2"><td class="colset-1">cell1</td><td class="colset-2">cell1</td></tr>
<tr class="rowset-3"><td class="colset-1">cell1</td><td class="colset-2">cell1</td></tr>
</table>';
//Search rowset-1,2,3 etc... to return number of rows
//Search colset-1,2,3 etc... to return number of cols
//or actually.. don't care about 1,2,3 - just get number of rows and nr of cols!
//
$nr_rows = substr_count( $table_data, 'rowset-');
$nr_cols = floor( substr_count( $table_data, 'colset-') / $nr_rows );https://stackoverflow.com/questions/65709958
复制相似问题