PHP 实现Office word 关键词添加背景色

需求:最近做一个word新闻规范扫描的工具,需要将wold中的内容读取出来扫描可疑、错误词文本,并将错误可疑文本添加背景颜色。
内容扫描规范识别不在本文中描述,重点说怎样通过编程语言操作word实现文字添加背景色

为了能快速达到效果,直接在https://github.com/PHPOffice/... 这个项目上扩展的功能:

clipboard.png

  • 在路径 phpoffice/phpword/src/PhpWord/ 中新建文件 Template.php
<?php

namespace PhpOffice\PhpWord;

class Template extends TemplateProcessor
{
    public $tempDocumentMainPart;

    public function __construct($documentTemplate)
    {
        parent::__construct($documentTemplate);
    }

    static $wordArr;
    static $color = 'yellow';

    /**
     * 多个词替换目前替换背景色功能
     *
     * @param $word
     * @param $color
     * @example {
     *  $template = new \PhpOffice\PhpWord\Template($path);
     *  $template->setWordBgColor($txt, 'yellow');
     * }
     */
    public function setWordArrBgColor($word, $color)
    {
        self::$wordArr = array_unique($word);
        if (!empty(self::$wordArr)) {

            self::$color  = $color;

            $this->tempDocumentHeaders = $this->_replace($this->tempDocumentHeaders);
            $this->tempDocumentMainPart = $this->_replace($this->tempDocumentMainPart);
            $this->tempDocumentFooters = $this->_replace($this->tempDocumentFooters);
        }
    }

    private function _replace($content) {

        return preg_replace_callback(
            '/<w:r w:([^>]*)>((?:(?!<\w:r>)[\s\S])*)<w:t[^>]*>((?:(?!<\/w:r>)[\s\S])*)<\/w:t><\/w:r[^>]*>/iUs',
            function ($matches) {
                // print_r($matches);
                if (!empty(trim($matches[3]))) {

                    $text = $matches[3];

                    foreach (self::$wordArr AS $value) {

                        // 判断关键词在字符串中是否存在
                        if (false !== strpos($text, $value)) {

                            // 背景色属性
                            $bgAttr = empty($matches[2])
                                ? '<w:rPr><w:highlight w:val="'.self::$color.'"/></w:rPr>'
                                : str_ireplace('</w:rPr>', '<w:highlight w:val="'.self::$color.'"/></w:rPr>', $matches[2]);

                            $matches[0] = str_ireplace($value,
                                '</w:t></w:r><w:r w:'.$matches[1].'>'.$bgAttr.'<w:t>'.$value.'</w:t></w:r><w:r w:'.$matches[1].'>'.$bgAttr.'<w:t>',
                                $matches[0]);
                        }
                    }


                    if (!empty($matches[0])) {

                        // 过滤掉空的
                        $matches[0] = preg_replace('/<w:r w:[^>]*>(?:(?!<\w:t>)[\s\S])*<w:t[^>]*><\/w:t><\/w:r[^>]*>/iUs', '', $matches[0]);
                    }
                }
                return $matches[0];
            },
            $content);
    }
}
  • 第二部就扩展完成背景色替换功能,接下怎样调用?
//引入类库
require autoload.php
$path = './test.docx';
$template = new \PhpOffice\PhpWord\Template($path);
$template->setWordArrBgColor(['TMD', '台湾省', 'Caonima'], 'yellow');
  • 效果

clipboard.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHPWord Beta 0.6.2 开发者指南 目 录 首先我们要了解文档最基本的信息和设置: 4 计量单位:缇(twips) 4 字体设置 4 文档属性设置 4 新建文档 5 添加页面 5 页面样式 5 页面样式属性 6 文本 7 添加文本 7 添加文本资源 7 文本样式 8 样式属性列表 9 添加换行符 10 添加分页符 10 列表 10 添加列表 10 列表样式 11 列表样式属性列表 11 超链接 11 添加超链接 11 超链接样式 12 图片 13 添加图片 13 图片样式 13 图片样式属性 13 添加GD生成图片 14 添加水印 14 添加对象 15 添加标题 15 添加目录 16 表格 17 添加表格 17 添加行 17 添加单元格 17 单元格样式 19 表格样式 20 页脚 22 页眉 23 模版 23 其他问题修改 25 解决文本缩进问题 25 表格对齐和表格缩进 27 图片缩进和绝对相对悬浮定位 30 首先我们要了解文档最基本的信息和设置:  因为是国外编辑的类库,存在对文支持的问题,使用前,我们需要进行一些修正: 1、解决编码问题,PHPword 会对输入的文字进行utf8_encode编码转化,如果你使用GBK、GB2312或者utf8编码的话就会出现乱码,如果你用utf8编码,就查找类库所有方法的 utf8_encode 转码将其删除,如果你采用GBK或者GB2312编码,使用iconv进行编码转换。 2、解决文字体支持,在writer/word2007/base.php 312行添加 $objWriter->writeAttribute('w:eastAsia',$font) 3、启动php zip支持,windows环境下在php配置文件php.ini,将extension=php_zip.dll前面的分号“;”去除;(如果没有,请添加extension=php_zip.dll此行并确保php_zip.dll文件存在相应的目录),然后同样在php.ini文件,将 zlib.output_compression = Off 改为zlib.output_compression = On ; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值