title: Import data from HTML Excel file using PHP
body: <p>I need to import the following excel file content into database using PHP script. It seems the excel file using none-standard format, so what is the best practice to do the task.</p>
  <pre><code>&lt;html xmlns:x=&quot;urn:schemas-microsoft-com:office:excel&quot; xmlns=&quot;http://www.w3.org/TR/REC-html40&quot;&gt;
  
  &lt;head&gt;
      &lt;!--[if gte mso 9]&gt;&lt;xml&gt;&lt;x:ExcelWorkbook&gt;&lt;x:ExcelWorksheets&gt;&lt;x:ExcelWorksheet&gt;&lt;x:Name&gt;List&lt;/x:Name&gt;&lt;x:WorksheetOptions&gt;&lt;x:Selected/&gt;&lt;/x:WorksheetOptions&gt;&lt;/x:ExcelWorksheet&gt;&lt;/x:ExcelWorksheets&gt;&lt;/x:ExcelWorkbook&gt;&lt;/xml&gt;&lt;![endif])--&gt;
      &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html;charset=utf-8&quot;&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;th&gt;#&lt;/th&gt;
              &lt;th&gt;&amp;#1585;&amp;#1602;&amp;#1605; &amp;#1575;&amp;#1604;&amp;#1591;&amp;#1575;&amp;#1604;&amp;#1576;&lt;/th&gt;
              &lt;th&gt;&amp;#1575;&amp;#1587;&amp;#1605; &amp;#1575;&amp;#1604;&amp;#1591;&amp;#1575;&amp;#1604;&amp;#1576;&lt;/th&gt;
              &lt;th&gt;&amp;#1575;&amp;#1604;&amp;#1581;&amp;#1575;&amp;#1604;&amp;#1577;&lt;/th&gt;
              &lt;th&gt;&amp;#1575;&amp;#1604;&amp;#1583;&amp;#1585;&amp;#1580;&amp;#1577;&lt;/th&gt;
              &lt;th&gt;&amp;#1605;&amp;#1604;&amp;#1575;&amp;#1581;&amp;#1592;&amp;#1575;&amp;#1578;&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;1&lt;/td&gt;
              &lt;td&gt;2141116724&lt;/td&gt;
              &lt;td&gt;&amp;#1588;&amp;#1607;&amp;#1583; &amp;#1587;&amp;#1593;&amp;#1583; &amp;#1605;&amp;#1606;&amp;#1575;&amp;#1581;&amp;#1609;
                  &amp;#1575;&amp;#1604;&amp;#1602;&amp;#1581;&amp;#1591;&amp;#1575;&amp;#1606;&amp;#1609; &lt;/td&gt;
              &lt;td&gt;&amp;#1605;&amp;#1587;&amp;#1580;&amp;#1604;&lt;/td&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;54&lt;/td&gt;
              &lt;td&gt;2192131354&lt;/td&gt;
              &lt;td&gt;&amp;#1593;&amp;#1584;&amp;#1575;&amp;#1585;&amp;#1610; &amp;#1584;&amp;#1610;&amp;#1575;&amp;#1576; &amp;#1605;&amp;#1581;&amp;#1605;&amp;#1608;&amp;#1583;
                  &amp;#1575;&amp;#1604;&amp;#1578;&amp;#1608;&amp;#1581;&amp;#1610;&amp;#1583; &lt;/td&gt;
              &lt;td&gt;&amp;#1605;&amp;#1587;&amp;#1580;&amp;#1604;&lt;/td&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>I used this code to extract the html code, Now need to skip all columns except the second and third col.</p>
  <pre><code>    $file = $_FILES[&quot;file&quot;][&quot;tmp_name&quot;];
      $file_open = fopen($file,&quot;r&quot;);
      $html = file($file);
  
      preg_match_all('#&lt;tr[^&gt;]*&gt;(.*?)&lt;/tr&gt;#is', $html[0], $lines);
      
      $result = array();
      
      foreach ($lines[1] as $k =&gt; $line) {
          preg_match_all('#&lt;td[^&gt;]*&gt;(.*?)&lt;/td&gt;#is', $line, $cell);
      
          foreach ($cell[1] as $cell) {
              $result[$k][] = trim($cell);
          }
      }
      
      var_dump($result);
  </code></pre>