PHPDocx Find and Replace Issues

236 views Asked by At

I am using PHPDocx to find and replace sentances in word docs, but It is not working for any string that includes a ' or a ". I have tried escaping these characters with no luck. Any ideas?

Exaple:

You’re being enrolled today.

will not be replaced, but

You are being enrolled today. 

will work fine.

1

There are 1 answers

0
edi9999 On

I think It is probably because the text is splitted amongst many tags, eg your text looks like this:

<w:t>You</w:t>
<w:t>'re being enrolled today</w:t>

That is one reason why it is difficult to do proper search/replace in word.

One way to get around this is to write thing in one row (eg not edit the sentence to add the ').

So If you delete the sentence and write it all again, I suspect it might work.

If that still doesn't work, you can also unzip your docx, and go to that sentence in the file word/document.xml and fix the problem with the tags yourself.

If you don't want to encounter that problem at all, you can use a templating engine I created for docx, but works in Node.JS or with the command line: https://github.com/edi9999/docxtemplater