问题描述
当前,我正在使用以下内容替换docx文件中的“邮件合并”字段。这会将邮件合并字段 «Address_1»
, «Address_2»
等替换为数据库中的值,然后将其输出为新的文档文件。
此刻我遇到的问题是,如果数据库中的值为空,则邮件合并字段将被替换为空白值,这又使空白行保持在原处,从而导致我输出文件:
private function __parseFile($thefile)
{
$this->_multicount++;
$content = file_get_contents($thefile); ///this is document.xml extracted from the docx file
foreach ($this->searchlist as $placeholder => $val) {
if($val=='' || empty($val)){
$content = str_replace('«'.$placeholder.'»','',$content); ;
}else{
$content = str_replace('«'.$placeholder.'»',htmlspecialchars($val),$content);
}
}
$newfile = $thefile.'.new'.($this->_multicount);
$fh = fopen($newfile,'wb');
fwrite($fh,$content);
fclose($fh);
}
该怎么办?当数据库中的值为空时,有一种更好的方法可以删除合并字段,以消除输出doc文件中的空行/段落。
解决方法
这是您可以通过使用字段规则在Word中轻松解决的问题。您将它们全部放在一行中并定义规则。
如果该字段不为空,则打印换行符。