PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.27k stars 2.7k forks source link

replaceBlock didn't work in office 2016 #1202

Open sunchenweb opened 6 years ago

sunchenweb commented 6 years ago

the funciton replaceBlock in Temproccessor didn't work,debug info below:

the regex is this: (<\?xml.*)(<w:p.*>\${block_table}<\/w:.*?p>)(.*)(<w:p.*\${\/block_table}<\/w:.*?p>) and the document.xml like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n

<w:document
    xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
    xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex"
    xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex"
    xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex"
    xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex"
    xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex"
    xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex"
    xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex"
    xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex"
    xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink"
    xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
    xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
    xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
    xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
    xmlns:w10="urn:schemas-microsoft-com:office:word"
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
    xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
    xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
    xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid"
    xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex"
    xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
    xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
    xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
    xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14">
    <w:background w:color="FFFFFF"/>
    <w:body>
        <w:p w:rsidR="00922228" w:rsidRDefault="00922228" w:rsidP="00922228">
            <w:pPr>
                <w:jc w:val="center"/>
                <w:rPr>
                    <w:sz w:val="44"/>
                    <w:szCs w:val="44"/>
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:rFonts w:hint="eastAsia"/>
                    <w:sz w:val="44"/>
                    <w:szCs w:val="44"/>
                </w:rPr>
                <w:t>月</w:t>
            </w:r>
            <w:r w:rsidRPr="00B540A2">
                <w:rPr>
                    <w:rFonts w:hint="eastAsia"/>
                    <w:sz w:val="44"/>
                    <w:szCs w:val="44"/>
                </w:rPr>
                <w:t>度总结报告</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00197886" w:rsidRDefault="00197886" w:rsidP="00197886">
            <w:r>
                <w:t>${block_table}</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00197886" w:rsidRDefault="00197886" w:rsidP="00197886">
            <w:r>
                <w:t>${/block_table}</w:t>
            </w:r>
        </w:p>
        <w:sectPr w:rsidR="00C571E7" w:rsidRPr="00C571E7" w:rsidSect="00BD69D6">
            <w:headerReference w:type="default" r:id="rId7"/>
            <w:footerReference w:type="default" r:id="rId8"/>
            <w:pgSz w:w="11906" w:h="16838" w:code="9"/>
            <w:pgMar w:top="1440" w:right="1021" w:bottom="1440" w:left="1418" w:header="851" w:footer="992" w:gutter="0"/>
            <w:cols w:space="720"/>
            <w:titlePg/>
            <w:docGrid w:linePitch="312"/>
        </w:sectPr>
    </w:body>
</w:document>

how to fix it

nicoder commented 6 years ago

there is nothing inside your block (except whitespace)?

(between the two block_table placeholders)

is the xml you give the document before calling replaceBlock? or after?

when you say it did not work, do you mean that nothing changed in the document?

liborm85 commented 3 years ago

replaceBlock method is broken. I use this simple modification:

  public function replaceBlock($blockname, $replacement) {
    $this->tempDocumentMainPart = preg_replace(
      '/(\${' . $blockname . '})(.*?)(\${\/' . $blockname . '})/is',
      $replacement,
      $this->tempDocumentMainPart
    );
  }