Search code examples
phplaravelms-wordphpword

PHP word not replace text in doc


I am trying to make a replacement in a .docx document

public function replace($seach, $replace)
{
    $file = base_path() . '/public/templates/test.docx';

    $phpword = new \PhpOffice\PhpWord\TemplateProcessor($file);

    $phpword->setValue('%name%', 'Santosh');
    $phpword->setValue('{lastname}', 'Achari');
    $phpword->setValue('{name}', 'Achari');
    $phpword->setValue('{officeAddress}', 'Yahoo');

    $phpword->saveAs('edited.docx');
}

This code replace in { }, but not replace for %%


Solution

  • According to the documentation page about Template processing the { } brackets are indeed the default, and looking at the code for the setValue function no way seems to be built-in to use a different prefix and suffix.

    This issue shows one possible way, but it requires modifying the source of the PHPWord package itself.