首页 > PHP技术 PHP技术

将HTML中的table表格内容装换为数组

2022-04-06 PHP技术 5557人已围观 0 收藏

简介将HTML中的table表格内容装换为数组

将HTML中的table表格内容装换为数组

function tableHtmlToArray($html) {
  $table = preg_replace("'<table[^>]*?>'si", "", $html);
  $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 = str_replace(" ", "", $table);
  $table = str_replace(" ", "", $table);

  $table = explode('{tr}', $table);
  array_pop($table);
  foreach ($table as $key => $tr) {
     $td = explode('{td}', $tr);
     array_pop($td);
     $td_array[] = $td;
  }
 
  return $td_array;
}


很赞哦! (0)

文章评论

本栏推荐