欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何在php中替換word內(nèi)容

今天就跟大家聊聊有關(guān)如何在php中替換word內(nèi)容,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了烏魯木齊免費(fèi)建站歡迎大家使用!

php替換word內(nèi)容的方法:首先下載PHPWORD,并解壓縮放到項(xiàng)目下的“extend”中;然后加載word文件;最后使用“$loadtemplate->setValue('account', '');”方法替換account內(nèi)容即可。

PHP使用PHPWORD替換WORD文檔內(nèi)容

利用PHP來替換word里面的內(nèi)容。環(huán)境是TP5的情況

下載PHPWORD類,解壓縮放到項(xiàng)目下的,extend/,即:項(xiàng)目/extend/PHPWord

使用方法:

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內(nèi)容為123
$loadtemplate->setValue('password', '456');//替換password內(nèi)容為456
$loadtemplate->save('/yourpath/'.md5(time()) .'.docx');//存儲(chǔ)位置

途中替換內(nèi)容的時(shí)候發(fā)現(xiàn)亂碼,或者是替換不成功!

主要更改了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;
                }
            }
        }
    }

看完上述內(nèi)容,你們對(duì)如何在php中替換word內(nèi)容有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)頁(yè)名稱:如何在php中替換word內(nèi)容
文章URL:http://chinadenli.net/article30/ppddso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站網(wǎng)站收錄網(wǎng)站設(shè)計(jì)企業(yè)建站網(wǎng)站內(nèi)鏈微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)