Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding and replacing text with PHPWord

Tags:

phpword

Is it possible to just do a simple find and replace for text on a Word document using PHPWord? From what I've seen, the closest you can get is just ADDING text to a section, and you can't manipulate existing text except for font, etc. If not, is there anything free that I can use to do this?

like image 765
Someone Avatar asked Nov 25 '13 20:11

Someone


1 Answers

Install

composer require phpoffice/phpword

PHP code

include 'vendor/autoload.php';
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('test2.docx');
$templateProcessor->setValue('name', 'myvar');
$templateProcessor->saveAs('./xx.docx');

My test.docx just has the word: ${name}

like image 112
周秀坤 Avatar answered Sep 21 '22 07:09

周秀坤