What is the best way to extract text from a docx file using PHP while preserving tabs and paragraphs?

When extracting text from a docx file using PHP, it is important to preserve tabs and paragraphs to maintain the formatting of the document. One way to achieve this is by using a library like PHPWord, which allows you to read the contents of a docx file while retaining the original formatting. By utilizing PHPWord, you can easily extract text with tabs and paragraphs intact.

require 'vendor/autoload.php';

$phpWord = new \PhpOffice\PhpWord\PhpWord();

// Load the docx file
$docx = $phpWord->load('example.docx');

// Get the text with tabs and paragraphs preserved
$text = '';
foreach ($docx->getSections() as $section) {
    foreach ($section->getElements() as $element) {
        if ($element instanceof \PhpOffice\PhpWord\Element\TextRun) {
            $text .= $element->getText();
        }
    }
}

echo $text;