只需将 2 个函数添加到文件“TemplateProcessor.php”
public function setValueAdvanced($search_replace)
{
foreach ($this->tempDocumentHeaders as $index => $headerXML) {
$this->tempDocumentHeaders[$index] = $this->setValueForPartAdvanced($this->tempDocumentHeaders[$index], $search_replace);
}
$this->tempDocumentMainPart = $this->setValueForPartAdvanced($this->tempDocumentMainPart, $search_replace);
foreach ($this->tempDocumentFooters as $index => $headerXML) {
$this->tempDocumentFooters[$index] = $this->setValueForPartAdvanced($this->tempDocumentFooters[$index], $search_replace);
}
}
protected function setValueForPartAdvanced($documentPartXML, $search_replace)
{
$pattern = '/<w:t>(.*?)<\/w:t>/';
$rplStringBeginOffcetsStack = array();
$rplStringEndOffcetsStack = array();
$rplCleanedStrings = array();
$stringsToClean = array();
preg_match_all($pattern, $documentPartXML, $words, PREG_OFFSET_CAPTURE);
$bux_founded = false;
$searching_started = false;
foreach($words[1] as $key_of_words => $word)
{
$exploded_chars = str_split($word[0]);
foreach($exploded_chars as $key_of_chars => $char)
{
if ($bux_founded)
{
if ($searching_started)
{
if ($char == "}")
{
$bux_founded = false;
$searching_started = false;
array_push($rplStringEndOffcetsStack, ($word[1]+mb_strlen($word[0])+6));
}
}
else
{
if ($char == "{")
{
$searching_started = true;
}
else
{
$bux_founded = false;
array_pop($rplStringBeginOffcetsStack);
}
}
}
else
{
if ($char == "$")
{
$bux_founded = true;
array_push($rplStringBeginOffcetsStack, $word[1]-5);
}
}
}
}
for($index=0; $index<count($rplStringEndOffcetsStack); $index++)
{
$string_to_clean = substr($documentPartXML, $rplStringBeginOffcetsStack[$index], ($rplStringEndOffcetsStack[$index]-$rplStringBeginOffcetsStack[$index]));
array_push($stringsToClean, $string_to_clean);
preg_match_all($pattern, $string_to_clean, $words_to_concat);
$cleaned_string = implode("", $words_to_concat[1]);
$cleaned_string = preg_replace('/[${}]+/', '', $cleaned_string);
array_push($rplCleanedStrings, $cleaned_string);
}
for ($index=0; $index<count($rplCleanedStrings); $index++)
{
foreach($search_replace as $key_search => $replace)
{
if ($rplCleanedStrings[$index] == $key_search)
{
$documentPartXML = str_replace($stringsToClean[$index], "<w:t>".$replace."</w:t>", $documentPartXML);
break;
}
}
}
return $documentPartXML;
}
如何使用:使用数组作为函数“setValueAdvanced”的唯一参数,其中“key” - 我们要替换的单词,“value” - 我们要粘贴的短语。重要提示:在 MS Word 文件中,使用“${word_to_replace}”来“标记”我们要替换的单词,但数组的键应该是“word_to_replace”,没有“${}”
示例代码:
require_once 'PhpWord/Autoloader.php';
use PhpOffice\PhpWord\Autoloader;
use PhpOffice\PhpWord\Settings;
define('CLI', (PHP_SAPI == 'cli') ? true : false);
define('EOL', CLI ? PHP_EOL : '<br />');
define('SCRIPT_FILENAME', basename($_SERVER['SCRIPT_FILENAME'], '.php'));
define('IS_INDEX', SCRIPT_FILENAME == 'index');
Autoloader::register();
Settings::loadConfig();
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('your_file_name.docx');
$search_replace_array = array(
'msword_hello'=>'Hello', #inside a MS Word file ${msword_hello} will change to Hello
'msword_world'=>'World' #${msword_world} will change to World
);
$templateProcessor->setValueAdvanced($search_replace_array);
$templateProcessor->saveAs('your_file_name_changed.docx');