HTMLのTABLEから配列を生成するPHPスクリプト

HTMLからテーブルを切り出して配列に収めます。
$array[table][row][col] てな感じの三次元配列にします。
HTML中の2個目のTABLEで3行目の5列目なら
$array[1][2][4]というふうに。
rowspanには対応してますが、colspanには対応してません。
あと大文字のタグには対応してません。
うーん、汎用にはまだ遠いです。

function table_parse($res){
	//td 以外のAttributeを消したいんですけど
	$res = preg_replace('/(<[^td][a-zA-Z]*).*?>/','$1>',$res);
	//td以外は、だいたい消す・・・みたいな
	//table to array. table[0]==RequestStatus,table[1]==Content
	$res = preg_replace("/\s+/s","",$res);
	preg_match_all("/.*?<\/table>/s",$res,$tables,PREG_SET_ORDER);//table to array

	for( $t=0; $t.*?<\/tr>/s",$tables[$t][0],$rows[$t],PREG_SET_ORDER);

		for( $r=0; $r.*?<\/(?:td|th)>/s",$rows[$t][$r][0],$lines[$t][$r],PREG_SET_ORDER);
			//thも入れるようにしたけど。。。うーん

			for( $l=0; $l/s","|",$lines[$t][$r][$l][0]);
				$cell_data = preg_replace("/<.*?>/s","",$cell_data);
				if( $rflag ){// rowspan find 
					$b=0;
					while($b<$rowspan[1]){//run to rowspan num
						$cells[$t][$r+$b][$l] = $cell_data;
						$b++;
					}
				}else{//rowspan unfound
					while( !is_null($cells[$t][$r][$l+$nc]) ){
						//nullのcellまでの数を数える
						$nc++;
					}//rowspan で挿入されたcellをスキップ
					$cells[$t][$r][$l+$nc] = $cell_data;//do not overwrite
				}
			}//table data spread
		}//table row spread
	}//table spread

	return $cells;
}