-2

I wonder if it's possible to solve such kind of template parsing using RegEx-es I have text similar to:

'Hello, {{ Na me|{{Surname }}}}. Would you like some drink? {{CoffeePreferred? We have a special discount for coffee}} {{Tea Preferred? We have a special discount for tea}}'

First, I need to fix errors in variable names (variables must always come right after braces to special symbol like "?" or "|" or something else could be comma or dot - it means rules can be added in future).

Na me, Tea Preferred Surname are spelling errors of variable names. Also variable names should start with right after curly braces contain no spaces after - otherwise it's also an error.

Then I need to keep text as is.

But Dear friend, We have a special discount for coffee and We have a special discount for tea are simple text.

So my question is how can I replace unnecessary spaces withing underscores in variables and keep them in text? Also I want to make it in as fewer steps as possible (due to performance impact and my texts can take megabytes).

I expect my text to be:

'Hello, {{Na_me|{{Surname}}}}. Would you like some drink? {{CoffeePreferred? We have a special discount for coffee}} {{Tea_Preferred? We have a special discount for tea}}'

My first step was to extract content of braces:

preg_replace_callback(
    '/(\{\{)(.*?)(\}\})/ui',
    function ($matches) {
        echo $matches[2];
});

Then I tried to replace spaces by rules described above with no success. I tried to use assertions like /[\s](?!<=\|)/ and expressions like (\s(?!\|))|((?!\|)\s) Does anybody have any idea?

sly
  • 149
  • 1
  • 6
  • You say `variables always come right after braces to special symbol like "?" or "|" or something else` what do you mean by `something else`? – Toto Apr 27 '16 at 17:02
  • What must be the result for the given example? – Toto Apr 27 '16 at 17:05
  • @Toto, `variables always come right after braces` - It's a rule but user can break it and in this case I need to fix it (`trim` works fine for simple templates but when braces include braces it become a problem for me) `something else` means rules can be added or changed in future, for example, exclamation mark can have special meaning - so after it all spaces must be kept, before it must be replaced with underscore. I added a result in my question – sly Apr 28 '16 at 06:01

1 Answers1

1

Here is a way to do the job:

$str = preg_replace_callback(
    '/\{\{([^|?{}]+)/',
    function ($matches) {
        $ret = trim($matches[1]);
        return preg_replace('/ /', '_', $ret);
    },
    $str
);

After that you can remove all the braces:

$str = preg_replace('/[{}]+/', '', $str);
Toto
  • 89,455
  • 62
  • 89
  • 125