php替换word内容的方法:首先下载phpword,并解压缩放到项目下的“extend”中;然后加载word文件;最后使用“$loadtemplate->setvalue('account', '');”方法替换account内容即可。
php使用phpword替换word文档内容
利用php来替换word里面的内容。环境是tp5的情况
下载phpword类,解压缩放到项目下的,extend/,即:项目/extend/phpword
推荐:《php教程》
使用方法:
require_once './extend/phpword/phpword.php';require_once './extend/phpword/phpword/iofactory.php';$phpword = new phpword();$loadtemplate = $phpword->loadtemplate('/'.$template.'.docx';);//加载word文件$loadtemplate->setvalue('account', '123');//替换account内容为123$loadtemplate->setvalue('password', '456');//替换password内容为456$loadtemplate->save('/yourpath/'.md5(time()) .'.docx');//存储位置途中替换内容的时候发现乱码,或者是替换不成功!
主要更改了setvalue的替换方法,方法文件所在地:extend/phpword/phpword/template.php
更换为:
public function setvalue($search, $replace, $limit=-1) { if(substr($search, 0, 1) !== '{' && substr($search, -1) !== '}') { $search = '{'.$search.'}'; } if(!is_array($replace)) { // $replace = utf8_encode($replace); //$replace = iconv( 'gbk','utf-8', $replace); $replace = str_replace("\\\\n","<w:br />",$replace); } preg_match_all('/\\\\{[^}] \\\\}/', $this->_documentxml, $matches); foreach ($matches[0] as $k => $match) { $no_tag = strip_tags($match); if ($no_tag == $search) { $match = '{'.$match.'}'; $this->_documentxml = preg_replace($match, $replace, $this->_documentxml, $limit); if ($limit == 1) { break; } } } }
门户网站怎么赢利?门户网站的服务价值是什么?无损音乐播放器哪个音质最好 音质出众的无损音乐播放器推荐服务器租用是什么whois查询是什么意思国内免备案asp空间的优点是什么?好的域名申请需要什么资料 域名申请需要实名吗网站建设高端定制怎么做?需要什么条件短信验证已于年月号下午验证通过了