PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Allow to use another RegExp Delimiter

Open thomasb88 opened this issue 2 years ago • 2 comments

Is your feature request related to a problem? Please describe.

RegExp Delimiter appears to be hardcoded as /, whereas the Macro characters choice is free, and the blocks and variables name choice is free

Describe the solution you'd like

Add a class variable private $regexp_delimiter = '/';

and report it in preg_xxx function

Then provide a function to manage it as a parameter.

This would allow to adapt the regexp parameter looking to the file content, ie not to have to manage to escape the slash, but to escape a generic parameter use for example preg_quote function

Note that also as setMacroChars function is Provided, a test could be set to be sure the slash is escaped if included in it (preg_quote is used quite everywhere, but not on cloneBlock and replaceBlock).

Describe alternatives you've considered

1 -Forbid the slash in the block and variables OR 2 - Escape the slash in all the data that is provided to preg_xxx functions

Additional context

thomasb88 avatar Aug 26 '23 06:08 thomasb88

Note i see in PhpWord\Escaper\RegExp the following const REG_EXP_DELIMITER = '/';

But:

  • It can't be modified
  • It's not escaped in TemplateProcessor.php

thomasb88 avatar Aug 27 '23 01:08 thomasb88

I had similar issue with TemplateProcessor->setImageValue method. I had placeholders with / symbol, so I had to modify vendor files to add / as a second param for preg_quote():

$varsToReplace = array_filter($partVariables, function ($partVar) use ($searchString) {
    return ($partVar == $searchString) || preg_match('/^' . preg_quote($searchString, '/') . ':/', $partVar);
});

and

if (preg_match('/(<[^<]+>)([^<]*)(' . preg_quote($varNameWithArgsFixed, '/') . ')([^>]*)(<[^>]+>)/Uu', $partContent, $matches)) {

I'm not sure about the consequences, so didn't dare to submit a PR with this, but probably might have someone.

4unkur avatar Dec 28 '23 03:12 4unkur