Advertisement
sebbu

TABLE parser with regexps

Feb 26th, 2016
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>test HTML > TABLE</title>
  5. <style type="text/css">
  6. TABLE
  7. {
  8.     border: 1px black solid;
  9.     border-collapse: collapse;
  10. }
  11. TR
  12. {
  13.     height: 32px;
  14. }
  15. TD
  16. {
  17.     width: 32px;
  18.     text-align: center;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23.  
  24. <?php
  25. $data=file_get_contents('input.html');
  26.  
  27. preg_match_all('#<tr>(.*)</tr>#sU', $data, $matches);
  28.  
  29. $row=count($matches[0]);
  30. $col=-1;
  31.  
  32. $arr=array();
  33. for($i=0;$i<$row;$i++) $arr[$i]=array();
  34.  
  35. $i=0;
  36. foreach($matches[1] as $data2)
  37. {
  38.     $j=0;
  39.     $k=0;
  40.     preg_match_all('#<td(?:\s*(rowspan)="([^"]*)"|\s*(colspan)="([^"]*)")*>(.*)</td>#sU', $data2, $matches2);
  41.     if($col==-1) $col=count($matches[0]);
  42.     for($k=0;$k<count($matches2[0]);$k++)
  43.     {
  44.         $row2=1;
  45.         $col2=1;
  46.         while(array_key_exists($j, $arr[$i]) && $arr[$i][$j]!==false) $j++;
  47.         $data2 = $matches2[5][$k];
  48.         if($matches2[1][$k]=='rowspan') $row2=$matches2[2][$k];
  49.         if($matches2[1][$k]=='colspan') $col2=$matches2[2][$k];
  50.         if($matches2[3][$k]=='rowspan') $row2=$matches2[4][$k];
  51.         if($matches2[3][$k]=='colspan') $col2=$matches2[4][$k];
  52.         for($a=0;$a<$row2;$a++)
  53.         {
  54.             for($b=0;$b<$col2;$b++)
  55.             {
  56.                 $arr[$i+$a][$j+$b]=$data2;
  57.             }
  58.         }
  59.         $j++;
  60.     }
  61.     $i++;
  62. }
  63.  
  64. //tri du tableau (les clés sont pas dans le bon ordre)
  65. ksort($arr);
  66. for($i=0;$i<count($arr);$i++) ksort($arr[$i]);
  67.  
  68. //var_dump($arr);
  69.  
  70. echo '<table border="1">'."\r\n";
  71. foreach($arr as $line)
  72. {
  73.     echo "\t".'<tr>'."\r\n";
  74.     foreach($line as $cell)
  75.     {
  76.         echo "\t\t".'<td>'.$cell.'</td>'."\r\n";
  77.     }
  78.     echo "\t".'</tr>'."\r\n";
  79. }
  80. echo '</table>'."\r\n";
  81. ?>
  82.  
  83. </body>
  84. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement