79343606

Date: 2025-01-09 18:06:14
Score: 3
Natty:
Report link

Thanks for the advices and for the alternatives, I finally came up with a solution though. This is the Regular expression that worked for me:

{{\s*(\w*)\s*}}(?=[^%]*%})

Explanation:
{{\s*(\w*)\s*}} Matches anything inside the {{ }} delimiters including any number of leading or trailing blank spaces inside
        (\w*) matches words with common variables naming convention (i.e. letters, digits or underscores), and puts it in a capture group
(?=[^%]*%}) looks ahead to match ONLY if the condition is true (i.e. it matches only if the {{variable}} is nested inside the {% %} delimiters)
        [^%]* matches each of the rest of the characters in the string to check that ARE NOT a % (percent character)...
        %} matches the end of the {% %} code block

This is the final SINGLE LINE preg_replace() statement achieving the originally desired output:

echo preg_replace('#{{\s*(\w*)\s*}}(?=[^%]*%})#', '\$$1', $string);

You can confirm the regex here and the replacements here

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Whitelisted phrase (-1): worked for me
  • RegEx Blacklisted phrase (3): Thanks for the advices
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Jod