/**
* 将html表格转化为数组
* @param $table Html表格字符串
* @return mixed 数组
*/
public function get_td_array($table) {
$table = preg_replace("'<table[^>]*?>'si","",$table);
$table = preg_replace("'<tr[^>]*?>'si","",$table);
$table = preg_replace("'<td[^>]*?>'si","",$table);
$table = str_replace("</tr>","{tr}",$table);
$table = str_replace("</td>","{td}",$table);
//去掉 HTML 标记
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
//去掉空白字符
$table = preg_replace("'([rn])[s]+'","",$table);
$table = preg_replace('/ /',"",$table);
$table = str_replace(" ","",$table);
$table = str_replace("\r\n","",$table);
$table = explode('{tr}', $table);
array_pop($table);
$td_array = [];
foreach ($table as $key=>$tr) {
$td = explode('{td}', $tr);
array_pop($td);
foreach ( $td as $k => $v ) {
$td[$k] = trim( $v );
}
$td_array[] = $td;
}
return $td_array;
}
PHP将html表格转化为数组
最新推荐文章于 2024-04-11 20:36:51 发布