Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use another RegExp Delimiter #2442

Open
thomasb88 opened this issue Aug 26, 2023 · 2 comments
Open

Allow to use another RegExp Delimiter #2442

thomasb88 opened this issue Aug 26, 2023 · 2 comments

Comments

@thomasb88
Copy link

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
Copy link
Author

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

@4unkur
Copy link

4unkur commented Dec 28, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants