PhpWord不替换文本 [英] PhpWord doesn't replace text
问题描述
我有一个docx文件,我需要替换一些文本.这是在codeigniter框架内完成的;这是代码:
I have a docx file and I need to replace some text. This is done inside codeigniter framework; here is the code:
$this->load->library('word');
$template = $this->word->loadTemplate($_SERVER['DOCUMENT_ROOT'].'/doc/assets/doc3.docx');
$template->setValue('replacename', 'new');
$template->save($_SERVER['DOCUMENT_ROOT'].'/doc/assets/helloWorld.docx');
当我打开新文件时,我仍然会收到"new"的"replacename" instad.替换名称"使用Verdana字体,9pt字体大小格式化(没有下划线或粗体).为什么不起作用?从setValue函数(和doc文件)中删除$ {}可以正常工作
When I open the new file I still get "replacename" instad of "new". "replacename" is formatted with Verdana font, 9pt font size (no underline or bold). Why it doesn't work? Removing ${ } from setValue function (and from doc file) it works
推荐答案
事实证明,当您将文本添加到word文件时,有时会在基础xml文件中创建额外的标签.因此(伪代码) $ {NAME}
可以变成< tag1> $ {</tag1>< tag2> NAME</tag2>< tag3>}}</tag3>
It turns out that when you add text to a word file, it sometimes creates extra tags in the underlying xml file. So (pseudo code) ${NAME}
can become <tag1>${</tag1><tag2>NAME</tag2><tag3>}</tag3>
因此,phpword找不到您的针头.
Because of this, phpword can't find your needle.
我正在使用带有Microsoft Word的Mac,我可以剪切Word文件的所有内容,然后再次粘贴.
I'm using a mac with microsoft word and i can just cut all the contents of the word file and paste them again.
然后Word将重新创建其中$ {NAME}是1标记而不是3的基础xml文件.
Word will than recreate the underlying xml file in which ${NAME} is 1 tag instead of 3.
这篇关于PhpWord不替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!