How do I extract the text content from a word document with PHP?
<p>I want to extract the text content from the word document with PHP.</p>

<p>I have created a new word document in Microsoft Word for Mac 2011.
Edit: have also tested by creating the same document in Microsoft Word under Windows 7.</p>

<p>The contents of the document is </p>

<pre><code>The quick brown fox jumps over the lazy dog
</code></pre>

<p>I have saved it to disk as a Word 97-2004 Document (.doc).</p>

<p>I'm using <a href="https://github.com/PHPOffice/PHPWord" rel="noreferrer">phpoffice/phpword</a> and this code to extract the text:</p>

<pre><code>&lt;?php

$source = "word.doc";

$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'MsDoc');

$text = '';

$sections = $phpWord-&gt;getSections();

foreach ($sections as $s) {
    $els = $s-&gt;getElements();
    foreach ($els as $e) {
        if (get_class($e) === 'PhpOffice\PhpWord\Element\Text') {
            $text .= $e-&gt;getText();
        } elseif (get_class($e) === 'PhpOffice\PhpWord\Section\TextBreak') {
            $text .= " \n";
        } else {
            throw new Exception('Unknown class type ' . get_class($e));
        }
    }
}

print $text;
</code></pre>

<p>The output of this code is only parts of the text:</p>

<pre><code>The quick brown fox j
</code></pre>

<p>Is there a problem with the code, or is it some kind of compatibility issue?</p>

<p>Edit:</p>

<p>If I add a <code>var_dump($els);</code> before <code>foreach ($els as $e) {</code> the output is this:</p>

<pre><code>array(1) {
  [0]=&gt;
  object(PhpOffice\PhpWord\Element\Text)#1265 (14) {
    ["text":protected]=&gt;
    string(21) "The quick brown fox j"
    ["fontStyle":protected]=&gt;
    object(PhpOffice\PhpWord\Style\Font)#1267 (25) {
      ["aliases":protected]=&gt;
      array(1) {
        ["line-height"]=&gt;
        string(10) "lineHeight"
      }
      ["type":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      string(4) "text"
      ["name":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      NULL
      ["hint":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      NULL
      ["size":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      NULL
      ["color":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      NULL
      ["bold":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      bool(false)
      ["italic":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      bool(false)
      ["underline":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      string(4) "none"
      ["superScript":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      bool(false)
      ["subScript":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      bool(false)
      ["strikethrough":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      bool(false)
      ["doubleStrikethrough":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      bool(false)
      ["smallCaps":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      bool(false)
      ["allCaps":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      bool(false)
      ["fgColor":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      NULL
      ["scale":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      NULL
      ["spacing":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      NULL
      ["kerning":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      NULL
      ["paragraph":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      object(PhpOffice\PhpWord\Style\Paragraph)#1266 (26) {
        ["aliases":protected]=&gt;
        array(1) {
          ["line-height"]=&gt;
          string(10) "lineHeight"
        }
        ["basedOn":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        string(6) "Normal"
        ["next":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        NULL
        ["alignment":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        string(0) ""
        ["indentation":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        NULL
        ["spacing":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        NULL
        ["lineHeight":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        NULL
        ["widowControl":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        bool(true)
        ["keepNext":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        bool(false)
        ["keepLines":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        bool(false)
        ["pageBreakBefore":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        bool(false)
        ["numStyle":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        NULL
        ["numLevel":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        int(0)
        ["tabs":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        array(0) {
        }
        ["shading":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
        NULL
        ["borderTopSize":protected]=&gt;
        NULL
        ["borderTopColor":protected]=&gt;
        NULL
        ["borderLeftSize":protected]=&gt;
        NULL
        ["borderLeftColor":protected]=&gt;
        NULL
        ["borderRightSize":protected]=&gt;
        NULL
        ["borderRightColor":protected]=&gt;
        NULL
        ["borderBottomSize":protected]=&gt;
        NULL
        ["borderBottomColor":protected]=&gt;
        NULL
        ["styleName":protected]=&gt;
        NULL
        ["index":protected]=&gt;
        NULL
        ["isAuto":"PhpOffice\PhpWord\Style\AbstractStyle":private]=&gt;
        bool(false)
      }
      ["shading":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      NULL
      ["rtl":"PhpOffice\PhpWord\Style\Font":private]=&gt;
      bool(false)
      ["styleName":protected]=&gt;
      NULL
      ["index":protected]=&gt;
      NULL
      ["isAuto":"PhpOffice\PhpWord\Style\AbstractStyle":private]=&gt;
      bool(false)
    }
    ["paragraphStyle":protected]=&gt;
    object(PhpOffice\PhpWord\Style\Paragraph)#1266 (26) {
      ["aliases":protected]=&gt;
      array(1) {
        ["line-height"]=&gt;
        string(10) "lineHeight"
      }
      ["basedOn":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      string(6) "Normal"
      ["next":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      NULL
      ["alignment":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      string(0) ""
      ["indentation":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      NULL
      ["spacing":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      NULL
      ["lineHeight":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      NULL
      ["widowControl":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      bool(true)
      ["keepNext":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      bool(false)
      ["keepLines":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      bool(false)
      ["pageBreakBefore":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      bool(false)
      ["numStyle":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      NULL
      ["numLevel":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      int(0)
      ["tabs":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      array(0) {
      }
      ["shading":"PhpOffice\PhpWord\Style\Paragraph":private]=&gt;
      NULL
      ["borderTopSize":protected]=&gt;
      NULL
      ["borderTopColor":protected]=&gt;
      NULL
      ["borderLeftSize":protected]=&gt;
      NULL
      ["borderLeftColor":protected]=&gt;
      NULL
      ["borderRightSize":protected]=&gt;
      NULL
      ["borderRightColor":protected]=&gt;
      NULL
      ["borderBottomSize":protected]=&gt;
      NULL
      ["borderBottomColor":protected]=&gt;
      NULL
      ["styleName":protected]=&gt;
      NULL
      ["index":protected]=&gt;
      NULL
      ["isAuto":"PhpOffice\PhpWord\Style\AbstractStyle":private]=&gt;
      bool(false)
    }
    ["phpWord":protected]=&gt;
    object(PhpOffice\PhpWord\PhpWord)#1247 (3) {
      ["sections":"PhpOffice\PhpWord\PhpWord":private]=&gt;
      array(1) {
        [0]=&gt;
        object(PhpOffice\PhpWord\Element\Section)#1261 (16) {
          ["container":protected]=&gt;
          string(7) "Section"
          ["style":"PhpOffice\PhpWord\Element\Section":private]=&gt;
          object(PhpOffice\PhpWord\Style\Section)#1262 (28) {
            ["orientation":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            string(8) "portrait"
            ["paper":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            object(PhpOffice\PhpWord\Style\Paper)#1263 (8) {
              ["sizes":"PhpOffice\PhpWord\Style\Paper":private]=&gt;
              array(6) {
                ["A3"]=&gt;
                array(3) {
                  [0]=&gt;
                  int(297)
                  [1]=&gt;
                  int(420)
                  [2]=&gt;
                  string(2) "mm"
                }
                ["A4"]=&gt;
                array(3) {
                  [0]=&gt;
                  int(210)
                  [1]=&gt;
                  int(297)
                  [2]=&gt;
                  string(2) "mm"
                }
                ["A5"]=&gt;
                array(3) {
                  [0]=&gt;
                  int(148)
                  [1]=&gt;
                  int(210)
                  [2]=&gt;
                  string(2) "mm"
                }
                ["Folio"]=&gt;
                array(3) {
                  [0]=&gt;
                  float(8.5)
                  [1]=&gt;
                  int(13)
                  [2]=&gt;
                  string(2) "in"
                }
                ["Legal"]=&gt;
                array(3) {
                  [0]=&gt;
                  float(8.5)
                  [1]=&gt;
                  int(14)
                  [2]=&gt;
                  string(2) "in"
                }
                ["Letter"]=&gt;
                array(3) {
                  [0]=&gt;
                  float(8.5)
                  [1]=&gt;
                  int(11)
                  [2]=&gt;
                  string(2) "in"
                }
              }
              ["size":"PhpOffice\PhpWord\Style\Paper":private]=&gt;
              string(2) "A4"
              ["width":"PhpOffice\PhpWord\Style\Paper":private]=&gt;
              int(11870)
              ["height":"PhpOffice\PhpWord\Style\Paper":private]=&gt;
              int(16787)
              ["styleName":protected]=&gt;
              NULL
              ["index":protected]=&gt;
              NULL
              ["aliases":protected]=&gt;
              array(0) {
              }
              ["isAuto":"PhpOffice\PhpWord\Style\AbstractStyle":private]=&gt;
              bool(false)
            }
            ["pageSizeW":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            int(11906)
            ["pageSizeH":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            int(16838)
            ["marginTop":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            int(1417)
            ["marginLeft":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            int(1417)
            ["marginRight":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            int(1417)
            ["marginBottom":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            int(1417)
            ["gutter":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            int(0)
            ["headerHeight":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            int(720)
            ["footerHeight":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            int(720)
            ["pageNumberingStart":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            NULL
            ["colsNum":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            int(1)
            ["colsSpace":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            int(720)
            ["breakType":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            NULL
            ["lineNumbering":"PhpOffice\PhpWord\Style\Section":private]=&gt;
            NULL
            ["borderTopSize":protected]=&gt;
            NULL
            ["borderTopColor":protected]=&gt;
            NULL
            ["borderLeftSize":protected]=&gt;
            NULL
            ["borderLeftColor":protected]=&gt;
            NULL
            ["borderRightSize":protected]=&gt;
            NULL
            ["borderRightColor":protected]=&gt;
            NULL
            ["borderBottomSize":protected]=&gt;
            NULL
            ["borderBottomColor":protected]=&gt;
            NULL
            ["styleName":protected]=&gt;
            NULL
            ["index":protected]=&gt;
            NULL
            ["aliases":protected]=&gt;
            array(0) {
            }
            ["isAuto":"PhpOffice\PhpWord\Style\AbstractStyle":private]=&gt;
            bool(false)
          }
          ["headers":"PhpOffice\PhpWord\Element\Section":private]=&gt;
          array(0) {
          }
          ["footers":"PhpOffice\PhpWord\Element\Section":private]=&gt;
          array(0) {
          }
          ["elements":protected]=&gt;
          array(1) {
            [0]=&gt;
            *RECURSION*
          }
          ["phpWord":protected]=&gt;
          *RECURSION*
          ["sectionId":protected]=&gt;
          int(1)
          ["docPart":protected]=&gt;
          string(7) "Section"
          ["docPartId":protected]=&gt;
          int(1)
          ["elementIndex":protected]=&gt;
          int(1)
          ["elementId":protected]=&gt;
          NULL
          ["relationId":protected]=&gt;
          NULL
          ["nestedLevel":"PhpOffice\PhpWord\Element\AbstractElement":private]=&gt;
          int(0)
          ["parentContainer":"PhpOffice\PhpWord\Element\AbstractElement":private]=&gt;
          NULL
          ["mediaRelation":protected]=&gt;
          bool(false)
          ["collectionRelation":protected]=&gt;
          bool(false)
        }
      }
      ["collections":"PhpOffice\PhpWord\PhpWord":private]=&gt;
      array(5) {
        ["Bookmarks"]=&gt;
        object(PhpOffice\PhpWord\Collection\Bookmarks)#1248 (1) {
          ["items":"PhpOffice\PhpWord\Collection\AbstractCollection":private]=&gt;
          array(0) {
          }
        }
        ["Titles"]=&gt;
        object(PhpOffice\PhpWord\Collection\Titles)#1249 (1) {
          ["items":"PhpOffice\PhpWord\Collection\AbstractCollection":private]=&gt;
          array(0) {
          }
        }
        ["Footnotes"]=&gt;
        object(PhpOffice\PhpWord\Collection\Footnotes)#1250 (1) {
          ["items":"PhpOffice\PhpWord\Collection\AbstractCollection":private]=&gt;
          array(0) {
          }
        }
        ["Endnotes"]=&gt;
        object(PhpOffice\PhpWord\Collection\Endnotes)#1251 (1) {
          ["items":"PhpOffice\PhpWord\Collection\AbstractCollection":private]=&gt;
          array(0) {
          }
        }
        ["Charts"]=&gt;
        object(PhpOffice\PhpWord\Collection\Charts)#1252 (1) {
          ["items":"PhpOffice\PhpWord\Collection\AbstractCollection":private]=&gt;
          array(0) {
          }
        }
      }
      ["metadata":"PhpOffice\PhpWord\PhpWord":private]=&gt;
      array(3) {
        ["DocInfo"]=&gt;
        object(PhpOffice\PhpWord\Metadata\DocInfo)#1253 (12) {
          ["creator":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          string(0) ""
          ["lastModifiedBy":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          string(0) ""
          ["created":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          int(1483515248)
          ["modified":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          int(1483515248)
          ["title":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          string(0) ""
          ["description":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          string(0) ""
          ["subject":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          string(0) ""
          ["keywords":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          string(0) ""
          ["category":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          string(0) ""
          ["company":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          string(0) ""
          ["manager":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          string(0) ""
          ["customProperties":"PhpOffice\PhpWord\Metadata\DocInfo":private]=&gt;
          array(0) {
          }
        }
        ["Protection"]=&gt;
        object(PhpOffice\PhpWord\Metadata\Protection)#1254 (1) {
          ["editing":"PhpOffice\PhpWord\Metadata\Protection":private]=&gt;
          NULL
        }
        ["Compatibility"]=&gt;
        object(PhpOffice\PhpWord\Metadata\Compatibility)#1255 (1) {
          ["ooxmlVersion":"PhpOffice\PhpWord\Metadata\Compatibility":private]=&gt;
          int(12)
        }
      }
    }
    ["sectionId":protected]=&gt;
    NULL
    ["docPart":protected]=&gt;
    string(7) "Section"
    ["docPartId":protected]=&gt;
    int(1)
    ["elementIndex":protected]=&gt;
    int(1)
    ["elementId":protected]=&gt;
    string(6) "5d531b"
    ["relationId":protected]=&gt;
    NULL
    ["nestedLevel":"PhpOffice\PhpWord\Element\AbstractElement":private]=&gt;
    int(0)
    ["parentContainer":"PhpOffice\PhpWord\Element\AbstractElement":private]=&gt;
    string(7) "Section"
    ["mediaRelation":protected]=&gt;
    bool(false)
    ["collectionRelation":protected]=&gt;
    bool(false)
  }
}
</code></pre>