Advertisement
sebbu

TABLE parser class with string functions

Feb 26th, 2016
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.44 KB | None | 0 0
  1. <?php
  2. class table_parser
  3. {
  4.     public $data;
  5.     public $total_rows=0;
  6.     public $total_cols=0;
  7.     public $rows;
  8.     public $cols;
  9.     public $cels;
  10.  
  11.     public function __construct( $data ) {
  12.         $this->data=$data;
  13.         $this->count();
  14.     }
  15.  
  16.     public function count()
  17.     {
  18.         $this->total_rows=substr_count($this->data,'</tr>');
  19.         $pos2=0;
  20.         $matches=array();
  21.        
  22.         for($i=0;$i<$this->total_rows;$i++) {
  23.             $pos1=strpos($this->data,'<tr',$pos2);
  24.             $pos2=strpos($this->data,'</tr>',$pos1);
  25.             $matches[]=substr($this->data,$pos1,$pos2+5-$pos1);
  26.         }
  27.         /* get number of columns */
  28.         foreach($matches as $value)
  29.         {
  30.             $cols=substr_count($value,'</td>');
  31.             if($cols>$this->total_cols)
  32.             {
  33.                 $this->total_cols=$cols;
  34.             }
  35.         }
  36.         /* get number of lines */
  37.         $this->total_rows=count($matches);
  38.        
  39.         /* initialize array */
  40.         $this->cels = array();
  41.         for($i=0; $i<$this->total_rows; $i++)
  42.         {
  43.             $this->cels[] = array();
  44.             for($j=0; $j<$this->total_cols; $j++)
  45.             {
  46.                 $this->cels[$i][] = false;
  47.             }
  48.         }
  49.         return array($this->total_rows,$this->total_cols);
  50.     }
  51.  
  52.     public function parse() {
  53.         // sauter la premiere ligne
  54.         //$pos2=strpos($this->data,'</tr>');
  55.         $pos2=0;
  56.         $rows=array();
  57.         for($i=0;$i<$this->total_rows;$i++)
  58.         {
  59.             $this->cols=0;
  60.             $this->rows=$i;
  61.             /* get line content */
  62.             $pos1=strpos($this->data,'<tr',$pos2);
  63.             $pos2=strpos($this->data,'</tr>',$pos1);
  64.             $rows[$i]=substr($this->data,$pos1,$pos2+5-$pos1);
  65.             $data=$rows[$i];
  66.             $cols=substr_count($data,'</td>');
  67.             $pos_2=0;$matches=array();
  68.             for($j=0;$j<$cols;$j++)
  69.             {
  70.                 /* isolate cell content */
  71.                 $pos_1=strpos($data,'<td',$pos_2);
  72.                 $pos_2=strpos($data,'</td>',$pos_1);
  73.                 $matches[$j]=substr($data,$pos_1,$pos_2+5-$pos_1);
  74.                 $pos__2=0;$pos__1=0;
  75.                 /* get cell size */
  76.                 if(substr_count($matches[$j],'colspan=')==1)
  77.                 {
  78.                     $pos__1=strpos($matches[$j],'colspan="',$pos__2)+9;
  79.                     $pos__2=strpos($matches[$j],'"',$pos__1);
  80.                     $colspan=substr($matches[$j],$pos__1,$pos__2-$pos__1);
  81.                 }
  82.                 else
  83.                 {
  84.                     $colspan="1";
  85.                 }
  86.                 $pos__2=0;
  87.                 if(substr_count($matches[$j],'rowspan=')==1)
  88.                 {
  89.                     $pos__1=strpos($matches[$j],'rowspan="',$pos__2);
  90.                     $pos__1+=9;
  91.                     $pos__2=strpos($matches[$j],'"',$pos__1);
  92.                     $rowspan=substr($matches[$j],$pos__1,$pos__2-$pos__1);
  93.                 }
  94.                 else
  95.                 {
  96.                     $rowspan="1";
  97.                 }
  98.                 /* get cell content */
  99.                 $pos__2=0;
  100.                 //$pos__1=strpos($matches[$j],'<a ',$pos__2);
  101.                 $pos__1=strpos($matches[$j],'>',$pos__1);
  102.                 //$pos__2=strpos($matches[$j],'</a>',$pos__1);
  103.                 $pos__2=strpos($matches[$j],'</td>',$pos__1);
  104.                 $cour=trim(substr($matches[$j],$pos__1+1,$pos__2-$pos__1-1));
  105.                 if($cour===false) $cour='';
  106.                 /* fill array */
  107.                 $num_1=0;$num_2=0;$num_1b=0;$num_2b=0;
  108.                 $num2add=0;
  109.                 for($num_1=0;$num_1<$rowspan;$num_1++)
  110.                 {
  111.                     $num_1b=$i+$num_1;
  112.                     for($num_2=0;$num_2<$colspan;$num_2++)
  113.                     {
  114.                         $num_2b=$this->cols+$num_2+$num2add;
  115.                         while($this->cels[$num_1b][$num_2b]!==false)
  116.                         {
  117.                                 $num2add++;
  118.                                 $num_2b=$num_2+$this->cols+$num2add;
  119.                         }
  120.                         //if($num_2b>23) die('érreur1');
  121.                         if($this->cels[$num_1b][$num_2b]!='')
  122.                         {
  123.                             var_dump($num_1b,$num_2b);
  124.                             die('érreur2');
  125.                         }
  126.                         $this->cels[$num_1b][$num_2b]=$cour;
  127.                     }
  128.                 }
  129.                 $this->cols+=$colspan-1;
  130.                 $cour='';
  131.                 $this->cols++;
  132.             }
  133.         }
  134.         return $this->cels;
  135.     }
  136.  
  137.     public function get_data() {
  138.         return $this->data;
  139.     }
  140. }
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement