<?php
  $crlf="\r\n";
  $cont=file_get_contents("from_word_to_html.html");
  $cont=str_replace('</span>','',str_replace('<span class="T1">','',str_replace('<p class="P1">',"\n",str_replace('</p>',"",explode('</p><p class="P1">*/</p></body></html>', str_replace(explode('>/*', $cont)[0] . '>','',$cont))[0]))));
  $recs=explode('*Table ', $cont);
  echo '' . sizeof($recs) . "\n\n";
  
  for ($i=1; $i<sizeof($recs); $i++) {
     $trec='';
     $numis=explode(' ', $recs[$i])[0];
     echo explode(' ', $recs[$i])[0] . "\n";
     $subrecs=explode("----+----", $recs[$i]);
     $subrecs[0]=explode('----', $subrecs[0])[0];
     $rest=explode("\n", explode($subrecs[0], $recs[$i])[1])[0] . "\n";
     $toprecs=explode("\n", '*Table ' . $subrecs[0] . $rest);
     $xrecs=explode("\n", explode('youwillveverfindthis', explode($subrecs[0] . $rest, $recs[$i])[1])[0]);
     $drecs=explode("\n", explode('----', explode($subrecs[0] . $rest, $recs[$i])[1])[0]);
     //$rest=explode('----',  explode(explode($subrecs[0], $recs[$i])[1] .  explode("\n",explode($subrecs[0], $recs[$i])[1])[0] . "\n", $recs[$i])[1]     )[0];
     if (substr($subrecs[0],-1,1) == "\n") { $subrecs[0]=substr($subrecs[0],0,(-1 + strlen($subrecs[0])));   }
     $thistitle='*Table ' . explode('/*', explode("\n", $subrecs[0])[0])[0];
     $thesefields=explode("\n", $subrecs[0])[-1 + sizeof(explode("\n", $subrecs[0]))];

       for ($jj=0; $jj<sizeof($xrecs); $jj++) {
         if (strpos($xrecs[$jj], 'Total') !== false) {
            $trec=$xrecs[$jj];
         }
       }
       
       if ($trec != '') {
        $dr=trim($trec);
       echo bin2hex($dr) . "\n";
       $dr=preg_replace('/[\x7F-\xFF]/ui', '', $dr);
       $drx='';
       $lastdrx=' ';
       echo "\n\n" . $dr . "\n\n";
       for ($jj=0; $jj<strlen($dr); $jj++) {
         if (substr(substr($dr,$jj),0,1) == '|') {
             $lastdrx='|';
             if (substr(substr($drx,$jj),0,1) != '|') {
             $drx.=substr(substr($dr,$jj),0,1);
             }
         } else if (substr(substr($dr,$jj),0,1) == ' ') {
           if ($lastdrx != '|' && substr(substr(($dr . 'x'),$jj),0,2) == '  ') {
             $drx.='|';
             $lastdrx='|';
           }
           if (substr(substr(($dr . 'x'),$jj),0,2) != '  ') {
             $drx.=' '; //substr(substr($dr,$jj),0,1);
             $lastdrx=' ';
           }
         } else if (ord(substr(substr($dr,$jj),0,1)) < 32) {
         } else {
             if (substr(substr($dr,$jj),0,1) != ',') {
             $drx.=substr(substr($dr,$jj),0,1);
             $lastdrx=substr(substr($dr,$jj),0,1);
             }
         }
       }
       if (substr($drx,0,1) == '|') {
       $trec=substr($drx,1);
       } else {
       $trec=$drx;
       }
      }

       $dr=trim($thesefields);
       $dr=preg_replace('/[\x7F-\xFF]/ui', '', $dr);
       $drx='';
       $lastdrx=' ';
       echo bin2hex($dr) . "\n";
       echo "\n\n" . $dr . "\n\n";
       for ($jj=0; $jj<strlen($dr); $jj++) {
         if (substr(substr($dr,$jj),0,1) == '|') {
             $lastdrx='|';
             if (substr(substr($drx,$jj),0,1) != '|') {
             $drx.=substr(substr($dr,$jj),0,1);
             }
         } else if (substr(substr($dr,$jj),0,1) == ' ') {
           if ($lastdrx != '|' && substr(substr(($dr . 'x'),$jj),0,2) == '  ') {
             $drx.='|';
             $lastdrx='|';
           }
           if (substr(substr(($dr . 'x'),$jj),0,2) != '  ') {
             $drx.=' '; //substr(substr($dr,$jj),0,1);
             $lastdrx=' ';
           }
         } else if (ord(substr(substr($dr,$jj),0,1)) < 32) {
         } else {
             if (substr(substr($dr,$jj),0,1) != ',') {
             $drx.=substr(substr($dr,$jj),0,1);
             $lastdrx=substr(substr($dr,$jj),0,1);
             }
         }
       }
       if (substr($drx,0,1) == '|') {
       $thesefields=substr($drx,1);
       } else {
       $thesefields=$drx;
       }



     for ($j=0; $j<sizeof($drecs); $j++) {
       $dr=str_replace(' |','|',($drecs[$j]));
       $dr=str_replace("\t", '|', $dr);
       //$dr=preg_replace('!\s+!', '|', $dr);
       for ($jj=0; $jj<strlen($dr); $jj++) {
         if (ord(substr(substr($dr,$jj),0,1)) < 32) {
           $dr=str_replace(substr(substr($dr,$jj),0,1), ' ', $dr);
         }
       }
       $dr=trim($dr);
       $dr=preg_replace('/[\x7F-\xFF]/ui', '', $dr);
       $drx='';
       $lastdrx=' ';
       echo bin2hex($dr) . "\n";
       echo "\n\n" . $dr . "\n\n";
       for ($jj=0; $jj<strlen($dr); $jj++) {
         if (substr(substr($dr,$jj),0,1) == '|') {
             $lastdrx='|';
             if (substr(substr($drx,$jj),0,1) != '|') {
             $drx.=substr(substr($dr,$jj),0,1);
             }
         } else if (substr(substr($dr,$jj),0,1) == ' ') {
           if ($lastdrx != '|' && substr(substr(($dr . 'x'),$jj),0,2) == '  ') {
             $drx.='|';
             $lastdrx='|';
           }
           if (substr(substr(($dr . 'x'),$jj),0,2) != '  ') {
             $drx.=' '; //substr(substr($dr,$jj),0,1);
             $lastdrx=' ';
           }
         } else if (ord(substr(substr($dr,$jj),0,1)) < 32) {
         } else {
             if (substr(substr($dr,$jj),0,1) != ',') {
             $drx.=substr(substr($dr,$jj),0,1);
             $lastdrx=substr(substr($dr,$jj),0,1);
             }
         }
       }
       if (substr($drx,0,1) == '|') {
       $drecs[$j]=substr($drx,1);
       } else {
       $drecs[$j]=$drx;
       }
     }
     
     $numcols=sizeof(explode('|', $drecs[0]));
     
     $fstuff="";
     $delmstuff='';
     for ($j=1; $j<$numcols; $j++) {
      $delmstuff.=',""';
     }
     //if ($numis == '10.95') {
     //  echo $drecs[0] . ' ... ' . $toprecs[0];
     //  exit;
     //}
     $fstuff.=str_replace("\t","",str_replace('" ','"',str_replace('" ','"',str_replace('"  ','"',str_replace('"    ','"',str_replace('"     ','"',str_replace(' "','"', str_replace('" ','"',   '"' . str_replace('|','","',$thesefields))))))))) . '"' . $crlf;
     for ($j=0; $j<$numcols; $j++) {
      //if (str_replace('"',' ',str_replace(","," ",$toprecs[$j])) != '/*') {
      $fstuff.='"' . str_replace('"',' ',str_replace(","," ",$toprecs[$j])) . '"' . $delmstuff . $crlf;
      //}
     }
     for ($j=0; $j<sizeof($drecs); $j++) {
      if (trim($drecs[$j]) != '') {
      $fstuff.=str_replace("\t","",str_replace('" ','"',str_replace('" ','"',str_replace('"  ','"',str_replace('"    ','"',str_replace('"     ','"',str_replace(' "','"', str_replace('" ','"',   '"' . str_replace('|','","',$drecs[$j]))))))))) . '"' . $crlf;
      }
     }
     
     if ($trec != '') {
      $idea=str_replace("\t","",str_replace('" ','"',str_replace('" ','"',str_replace('"  ','"',str_replace('"    ','"',str_replace('"     ','"',str_replace(' "','"', str_replace('" ','"',   '"' . str_replace('|','","',$trec))))))))) . '"';
      $ideas=explode(',', $idea);
      while (sizeof($ideas) < $numcols) {
        $idea.=',""';
        $ideas=explode(',', $idea);
      }
      $fstuff.= $idea . $crlf;
     }
     
     file_put_contents('table-' . str_replace('.','_',$numis) . '.csv', $fstuff);
     
     if (trim($thesefields) == '') { $thesefields=explode("\n", $subrecs[0])[-2 + sizeof(explode("\n", $subrecs[0]))];  }
     if (trim($thesefields) == '') { $thesefields=explode("\n", $subrecs[0])[-3 + sizeof(explode("\n", $subrecs[0]))];  }
     echo explode('/*',$thesefields)[-1 + sizeof(explode('/*',$thesefields))] . "\n";
     if (sizeof($subrecs) == 1) {
       echo "oops\n";
     } else if (sizeof($subrecs) == 2) {
       echo "Oops " . $i . "\n";
     }
  
  }
?>
