php 正则html表格数组保存csv与转换数组代码

  1. <?php
  2. //html表格的每行转为csv格式数组
  3. function get_tr_array($table) {
  4. //php开源代码
  5. $table = preg_replace("'<td[^>]*?>'si",'"',$table);
  6. $table = str_replace("</td>",'",',$table);
  7. $table = str_replace("</tr>","{tr}",$table); //开源代码phpfensi.com
  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. return $table;
  17. }
  18. ?>
  19. //将html表格的每行每列转为数组,采集表格数据
  20. <?
  21. function get_td_array($table) {
  22. $table = preg_replace("'<table[^>]*?>'si","",$table);
  23. //osphp.com.cn
  24. $table = preg_replace("'<tr[^>]*?>'si","",$table);
  25. $table = preg_replace("'<td[^>]*?>'si","",$table);
  26. $table = str_replace("</tr>","{tr}",$table); //开源代码phpfensi.com
  27. $table = str_replace("</td>","{td}",$table);
  28. //去掉 html 标记
  29. $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table); //phpfensi.com开源
  30. //去掉空白字符
  31. $table = preg_replace("'([rn])[s]+'","",$table);
  32. $table = str_replace(" ","",$table);//phpfensi.com
  33. $table = str_replace(" ","",$table);
  34. $table = explode('{tr}', $table);
  35. array_pop($table); //php开源代码
  36. foreach ($table as $key=>$tr) {
  37. $td = explode('{td}', $tr);
  38. array_pop($td);
  39. $td_array[] = $td; // }
  40. return $td_array;
  41. }
  42. ?>