首页 > 开发 > PHP > 正文

php将HTML表格每行每列转为数组实现采集表格数据的方法

2024-05-04 21:56:04
字体:
来源:转载
供稿:网友

这篇文章主要介绍了php将HTML表格每行每列转为数组实现采集表格数据的方法,涉及php正则替换的技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php将HTML表格每行每列转为数组实现采集表格数据的方法。分享给大家供大家参考。具体如下:

下面的php代码可以将HTML表格的每行每列转为数组,采集表格数据

  1. <?php 
  2. function get_td_array($table) { 
  3.   $table = preg_replace("'<table[^>]*?>'si","",$table); 
  4.   $table = preg_replace("'<tr[^>]*?>'si","",$table); 
  5.   $table = preg_replace("'<td[^>]*?>'si","",$table); 
  6.   $table = str_replace("</tr>","{tr}",$table); 
  7.   $table = str_replace("</td>","{td}",$table); 
  8.   //去掉 HTML 标记  
  9.   $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table); 
  10.   //去掉空白字符  
  11.   $table = preg_replace("'([rn])[s]+'","",$table); 
  12.   $table = str_replace(" ","",$table); 
  13.   $table = str_replace(" ","",$table); 
  14.   $table = explode('{tr}'$table); 
  15.   array_pop($table); 
  16.   foreach ($table as $key=>$tr) { 
  17.     $td = explode('{td}'$tr); 
  18.     array_pop($td); 
  19.     $td_array[] = $td
  20.   } 
  21.   return $td_array
  22. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表