title: How to fetch the innerHTML code from HTML table using PHP
body: <p>I am able to parse HTML page properly, but it is parsing just the data whereas I want to fetch entire HTML code inside in <code>&lt;tr&gt; , &lt;td&gt;</code>. Below is my PHP code:</p>
  
  <pre><code>&lt;?php    
     $dom = new DOMDocument();  
  
  //load the html  
  $html = $dom-&gt;loadHTMLFile("hydrocarbon.htm");  
  
    //discard white space   
  //$dom-&gt;preserveWhiteSpace = false;   
  
    //the table by its tag name  
  $tables = $dom-&gt;getElementsByTagName('table');   
  
  
      //get all rows from the table  
  $rows = $tables-&gt;item(0)-&gt;getElementsByTagName('tr');   
    // get each column by tag name  
  $cols = $rows-&gt;item(0)-&gt;getElementsByTagName('th');   
  $row_headers = NULL;
  foreach ($cols as $node) {
      //print $node-&gt;nodeValue."\n";   
      $row_headers[] = $node-&gt;nodeValue;
  }   
  
  $table = array();
    //get all rows from the table  
  $rows = $tables-&gt;item(0)-&gt;getElementsByTagName('tr');   
  foreach ($rows as $row)   
  {   
     // get each column by tag name  
      $cols = $row-&gt;getElementsByTagName('td');   
      $row = array();
      $i=0;
      foreach ($cols as $node) {
          # code...
          //print $node-&gt;nodeValue."\n";   
          if($row_headers==NULL)
              $row[] = $node-&gt;nodeValue;
          else
              $row[$row_headers[$i]] = $node-&gt;nodeValue;
          $i++;
      }   
      $table[] = $row;
  }   
  
  //var_dump($table);
  print("&lt;pre&gt;".print_r($table,true)."&lt;/pre&gt;");
  ?&gt;
  </code></pre>
  
  <p>This is my result:</p>
  
  <p><a href="https://i.stack.imgur.com/Lz6Aa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lz6Aa.png" alt="enter image description here"></a></p>
  
  <p>and this is my HTML code:</p>
  
  <pre><code>&lt;table&gt;
  &lt;thead&gt;
  &lt;tr&gt;&lt;th&gt;Column 1&lt;/th&gt;&lt;th&gt;Column 2&lt;/th&gt;&lt;th&gt;Column 3&lt;/th&gt;&lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
  &lt;tr&gt; &lt;td&gt;&lt;b&gt;Q&lt;/b&gt;&lt;/td&gt;&lt;td&gt;Desc.&lt;/td&gt; &lt;/tr&gt;
  &lt;tr&gt; &lt;td&gt;Type&lt;/td&gt;&lt;td&gt;Multiple choice&lt;/td&gt; &lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Option&lt;/td&gt;&lt;td&gt;image #####2&lt;/td&gt;&lt;td&gt;incorrect&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Option&lt;/td&gt;&lt;td&gt;image #####2&lt;/td&gt;&lt;td&gt;incorrect&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Option&lt;/td&gt;&lt;td&gt;image #####2&lt;/td&gt;&lt;td&gt;incorrect&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Option&lt;/td&gt;&lt;td&gt;image #####2&lt;/td&gt;&lt;td&gt;incorrect&lt;/td&gt;&lt;/tr&gt;
  
  &lt;tr&gt;&lt;td&gt;Solution&lt;/td&gt;&lt;td&gt;Some text / image&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Marks&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>It is parsing <code>Q</code> and not <code>&lt;b&gt;Q&lt;/b&gt;</code>. How can I achieve this?</p>
  
  <p><strong>Edit 1: Original table where your solution should work</strong></p>
  
  <pre><code>&lt;table class=MsoNormalTable border=1 cellspacing=0 cellpadding=0 width=610 style='width:457.25pt;margin-left:10.8pt;background:#CED7E7;border-collapse:
   collapse;border:none'&gt;
      &lt;tr style='height:30.35pt'&gt;
          &lt;td width=112 valign=top style='width:84.0pt;border:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:30.35pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;Question&lt;span style='border:none'&gt; &lt;/span&gt;&lt;/span&gt;
                  &lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=498 colspan=2 valign=top style='width:373.25pt;border:solid black 1.0pt;
    border-left:none;background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;
    height:30.35pt'&gt;
              &lt;p class=MsoNormal style='margin-top:0cm;margin-right:-48.45pt;margin-bottom:
    0cm;margin-left:18.0pt;margin-bottom:.0001pt;line-height:115%;border:none'&gt;&lt;b&gt;&lt;span
    lang=EN-US style='font-family:"Garamond","serif";border:none'&gt;&lt;span
    style='border:none'&gt;Consider the following reaction,&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;
              &lt;/p&gt;
              &lt;p class=MsoNormal style='margin-top:0cm;margin-right:-48.45pt;margin-bottom:
    0cm;margin-left:18.0pt;margin-bottom:.0001pt;line-height:115%'&gt;&lt;b&gt;&lt;span
    lang=EN-US style='font-family:"Garamond","serif";border:none'&gt;&lt;span
    style='border:none'&gt;H&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;sub&gt;&lt;span lang=EN-US
    style='font-family:"Garamond","serif";border:none'&gt;&lt;span style='border:none'&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/sub&gt;&lt;/b&gt;&lt;b&gt;&lt;span
    lang=EN-US style='font-family:"Garamond","serif";border:none'&gt;&lt;span
    style='border:none'&gt;C – CH – CH – CH&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;sub&gt;&lt;span
    lang=EN-US style='font-family:"Garamond","serif";border:none'&gt;&lt;span
    style='border:none'&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/sub&gt;&lt;/b&gt;&lt;b&gt;&lt;span lang=EN-US
    style='font-family:"Garamond","serif";border:none'&gt;&lt;span style='border:none'&gt;
    + &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span lang=EN-US style='font-family:"Garamond","serif";
    position:relative;top:2.0pt;border:none'&gt;&lt;img width=26 height=29
    src="hydrocarbon2_files/image001.png"&gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span lang=EN-US
    style='font-family:"Garamond","serif";border:none'&gt;&lt;span style='border:none'&gt; &amp;#8594;
    ‘X’  + HBr                                                   &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;
              &lt;/p&gt;
              &lt;p class=MsoNormal style='margin-top:0cm;margin-right:-48.45pt;margin-bottom:
    0cm;margin-left:18.0pt;margin-bottom:.0001pt;line-height:115%'&gt;&lt;b&gt;&lt;span
    lang=EN-US style='font-family:"Garamond","serif";border:none'&gt;&lt;span
    style='border:none'&gt;            |        |&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;
              &lt;/p&gt;
              &lt;p class=MsoNormal style='margin-top:0cm;margin-right:-48.45pt;margin-bottom:
    0cm;margin-left:18.0pt;margin-bottom:.0001pt;line-height:115%'&gt;&lt;b&gt;&lt;span
    lang=EN-US style='font-family:"Garamond","serif";border:none'&gt;&lt;span
    style='border:none'&gt;            D       CH&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;sub&gt;&lt;span
    lang=EN-US style='font-family:"Garamond","serif";border:none'&gt;&lt;span
    style='border:none'&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/sub&gt;&lt;/b&gt;
              &lt;/p&gt;
              &lt;p class=MsoNoSpacing style='margin-top:0cm;margin-right:-48.45pt;margin-bottom:
    0cm;margin-left:.3pt;margin-bottom:.0001pt;text-align:justify;text-indent:
    -.3pt'&gt;&lt;b&gt;&lt;span lang=EN-GB style='font-size:16.0pt;font-family:"Chaparral Pro","serif"'&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;
              &lt;/p&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style='height:15.0pt'&gt;
          &lt;td width=112 valign=top style='width:84.0pt;border:solid black 1.0pt;
    border-top:none;background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;
    height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;Type&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=498 colspan=2 valign=top style='width:373.25pt;border-top:none;
    border-left:none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;multiple_choice&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style='height:15.0pt'&gt;
          &lt;td width=112 valign=top style='width:84.0pt;border:solid black 1.0pt;
    border-top:none;background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;
    height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;Option&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=219 valign=top style='width:164.25pt;border-top:none;border-left:
    none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span style='font-size:16.0pt;color:black;border:none'&gt;&lt;img
    width=205 height=93 src="hydrocarbon2_files/image002.jpg"&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=279 valign=top style='width:209.0pt;border-top:none;border-left:
    none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;I&lt;/span&gt;&lt;/span&gt;&lt;span lang=EN-US style='font-size:16.0pt;
    border:none'&gt;&lt;span style='border:none'&gt;n&lt;span style='border:none'&gt;correct&lt;/span&gt;&lt;/span&gt;
                  &lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style='height:15.0pt'&gt;
          &lt;td width=112 valign=top style='width:84.0pt;border:solid black 1.0pt;
    border-top:none;background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;
    height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;Option&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=219 valign=top style='width:164.25pt;border-top:none;border-left:
    none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span style='font-size:16.0pt;border:none'&gt;&lt;img width=205
    height=102 id="Picture 13" src="hydrocarbon2_files/image003.jpg"&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=279 valign=top style='width:209.0pt;border-top:none;border-left:
    none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;C&lt;/span&gt;&lt;/span&gt;&lt;span lang=EN-US style='font-size:16.0pt;
    border:none'&gt;&lt;span style='border:none'&gt;orrect&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style='height:15.0pt'&gt;
          &lt;td width=112 valign=top style='width:84.0pt;border:solid black 1.0pt;
    border-top:none;background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;
    height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;Option&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=219 valign=top style='width:164.25pt;border-top:none;border-left:
    none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span style='font-size:16.0pt;border:none'&gt;&lt;img width=205
    height=107 id="Picture 16" src="hydrocarbon2_files/image004.jpg"&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=279 valign=top style='width:209.0pt;border-top:none;border-left:
    none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;Incorrect&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style='height:15.0pt'&gt;
          &lt;td width=112 valign=top style='width:84.0pt;border:solid black 1.0pt;
    border-top:none;background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;
    height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;Option&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=219 valign=top style='width:164.25pt;border-top:none;border-left:
    none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span style='font-size:16.0pt;border:none'&gt;&lt;img width=205
    height=112 id="Picture 19" src="hydrocarbon2_files/image005.jpg"&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=279 valign=top style='width:209.0pt;border-top:none;border-left:
    none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;Incorrect&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style='height:15.0pt'&gt;
          &lt;td width=112 valign=top style='width:84.0pt;border:solid black 1.0pt;
    border-top:none;background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;
    height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;Solution&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=498 colspan=2 valign=top style='width:373.25pt;border-top:none;
    border-left:none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=MsoNormal style='margin-left:27.0pt;text-align:justify;text-indent:
    -27.0pt;line-height:115%'&gt;&lt;span style='font-family:"Garamond","serif";
    border:none'&gt;&lt;img width=398 height=92 id="Picture 10"
    src="hydrocarbon2_files/image006.jpg"&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style='height:15.0pt'&gt;
          &lt;td width=112 valign=top style='width:84.0pt;border:solid black 1.0pt;
    border-top:none;background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;
    height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;Marks&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=219 valign=top style='width:164.25pt;border-top:none;border-left:
    none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;4&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
          &lt;td width=279 valign=top style='width:209.0pt;border-top:none;border-left:
    none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;
    background:transparent;padding:4.0pt 4.0pt 4.0pt 4.0pt;height:15.0pt'&gt;
              &lt;p class=BodyA&gt;&lt;span lang=EN-US style='font-size:16.0pt;border:none'&gt;&lt;span
    style='border:none'&gt;1&lt;/span&gt;&lt;/span&gt;
              &lt;/p&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>