PHPOffice / PHPWord

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

TemplateProcessor::replaceBlock regular expression not matching #523

Open skwi opened 9 years ago

skwi commented 9 years ago

Hello,

I have a non matching case in TemplateProcessor::replaceBlock method regular expression.

In the template document I use, it does not match the tag. Here is a sample of the document where you can see the tag.

<w:p w:rsidR="00D04554" w:rsidRDefault="00015D44" w:rsidP="00C57049"><w:pPr><w:jc w:val="both"/><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/></w:rPr><w:t>${</w:t></w:r><w:r w:rsidR="00D04554" w:rsidRPr="00D04554"><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/></w:rPr><w:t>ENSEMBLE_PERSONNEL</w:t></w:r><w:r w:rsidR="00A9712A"><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/></w:rPr><w:t>}</w:t></w:r></w:p>

<w:p w:rsidR="00D04554" w:rsidRDefault="007227A3" w:rsidP="00C57049"><w:pPr><w:jc w:val="both"/><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/></w:rPr></w:pPr><w:r w:rsidRPr="001A55F0"><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/><w:i/></w:rPr><w:t>L</w:t></w:r><w:r w:rsidR="002C759B" w:rsidRPr="001A55F0"><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/><w:i/></w:rPr><w:t>’ensemble d</w:t></w:r><w:r w:rsidR="001D4074" w:rsidRPr="001A55F0"><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/><w:i/></w:rPr><w:t>u personnel</w:t></w:r><w:r w:rsidR="002C759B" w:rsidRPr="001A55F0"><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/><w:i/></w:rPr><w:t xml:space="preserve"> salarié de la société</w:t></w:r><w:r w:rsidR="002C759B" w:rsidRPr="001A55F0"><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r></w:p>

<w:p w:rsidR="007227A3" w:rsidRPr="001A55F0" w:rsidRDefault="00015D44" w:rsidP="00C57049"><w:pPr><w:jc w:val="both"/><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/></w:rPr><w:t>${/</w:t></w:r><w:r w:rsidR="00D04554" w:rsidRPr="00D04554"><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/></w:rPr><w:t>ENSEMBLE_PERSONNEL</w:t></w:r><w:r w:rsidR="00A9712A"><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/></w:rPr><w:t>}</w:t></w:r></w:p>

The tag in the document is ${ENSEMBLE_PERSONNEL} [...] ${/ENSEMBLE_PERSONNEL}

I guess it's related to all the XML tags that Word inserted in the document but the template will be created by non technical users so I can tell them to fix it in the XML.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/11363092-templateprocessor-replaceblock-regular-expression-not-matching?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github).
Cbruyere commented 9 years ago

have you try to parse your xml online ? http://www.freeformatter.com/xml-formatter.html maybe it could be a helper :)

I've got : Unable to parse any XML input. org.jdom2.input.JDOMParseException: Error on line 1: The prefix "w" for element "w:p" is not bound.

skwi commented 9 years ago

The xml was generated by Office Word (and the file is properly opened, even after PHPWord manipulation) so I don't think it's badly formated.

w:p tags seems to be a valid OpenXML word, event if it's not valid in freeformater

skwi commented 9 years ago

I manage to make it work by removing the (<\?xml.*) part of the regex in the TemplateProcessor::replaceBlock method.

It looked like a line break at the beginning of the file made the regex fail. I edited the document with Word 12 on Mac OS 10.10, maybe that the reason for this failing line break.

I am going to work on a fork to se if I can come to a "clean" regex working with my document but keeping the (<\?xml.*).

samdark commented 7 years ago

Duplicate of #316

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue for you, please try to help by debugging it further and sharing your results. Thank you for your contributions.