【问题标题】:PHPWord - remove all search-patterns from word documentPHPWord - 从 word 文档中删除所有搜索模式
【发布时间】:2015-11-11 23:20:55
【问题描述】:

我正在使用 PHPWord 读取我的 docx 模板,并在处理结束时从模板中删除所有搜索模式。 docx 模板中的搜索模式是

${SOMETAG}
text block
${/SOMETAG}

docx 后面的 xml 结构看起来像这样:

<w:p w:rsidR="00E21534" w:rsidRDefault="00E21534" w:rsidP="00E21534"><w:r><w:t>${SOMETAG}</w:t></w:r></w:p><w:p w:rsidR="00E21534" w:rsidRDefault="00E21534" w:rsidP="00E21534"><w:r><w:t>text block</w:t></w:r></w:p><w:p w:rsidR="00E21534" w:rsidRDefault="00E21534" w:rsidP="00E21534"><w:r><w:t>${/SOMETAG}</w:t></w:r></w:p>

这是我为删除开始标记而编写的函数,即 ${SOMETAG} 但它似乎无法找到该标记。我认为问题在于我的 preg_match_all 中的模式。你能告诉我我在这里做错了什么吗?

public function removeSearchPatterns()
{
    //search for ${*}
    preg_match_all(
        '/(${.*})/is',
        $this->tempDocumentMainPart,
        $matches,
        PREG_SET_ORDER
    );

    //remove ${*}
    foreach ($matches as $match){
        if (isset($match[0])) {
            $this->tempDocumentMainPart = str_replace(
                $match[0],
                '',
                $this->tempDocumentMainPart
            );
        }
    }

}

【问题讨论】:

  • 如果将字符串模式更改为 '/\${\/?[a-zA-Z_-]*}/' 有效。这会删除以下模式:namePattern、name-pattern、name_pattern 和类似物

标签: preg-match-all docx phpword


【解决方案1】:

我已经根据您的代码创建了一个方法,但我简化为仅删除 ${} 和 ${/}。顺便说一句,感谢上面的代码。

public function removeSearchPatterns()
{
    //search for ${*} and ${/*}
    preg_match_all(
        '/(\$\{[^\}]*\})/',
        $this->temporaryDocumentMainPart,
        $matches,
        PREG_SET_ORDER
    );

    //remove ${*} and ${/*}
    foreach ($matches as $match){
        $this->temporaryDocumentMainPart = str_replace(
            $match,
            '',
            $this->temporaryDocumentMainPart
        );
    }
}

【讨论】:

    【解决方案2】:

    我做到了!尽管欢迎提出更有效方法的建议。

    public function removeSearchPatterns()
    {
        //search for ${*} and ${/*} 
        preg_match_all(
            '/(\${)(\b[^}]*)(})(.*?)(\${.)(\2)(})/',
            $this->temporaryDocumentMainPart,
            $matches,
            PREG_SET_ORDER
        );
        //if ${*} and ${/*} are found, do the removal
        if (!empty($matches)){
            //remove ${*} and ${/*}
            foreach ($matches as $match){               
                if (isset($match[0])) {
                    $this->temporaryDocumentMainPart = str_replace(
                        $match[0],
                        $match[4],
                        $this->temporaryDocumentMainPart
                    );
                }
            }
    
            //search for <w:p> to </w:p>
            preg_match_all(
                '/(<w:p[^>]*><w:r[^>]*><w:t><\/w:t><\/w:r>)(?:<[^<>]+><[^<>]+>)?(<\/w:p>)/',
                $this->temporaryDocumentMainPart,
                $matches,
                PREG_SET_ORDER
            );
    
            //remove <w:p> to </w:p>
            foreach ($matches as $match){
                if (isset($match[0])) {             
                    $this->temporaryDocumentMainPart = str_replace(
                        $match[0],
                        '',
                        $this->temporaryDocumentMainPart
                    );
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-10
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多