I have a templating system to show things based on $this->cms_language() value.
In this case, the $value will be replaced with something matched the regex pattern.
It works just as http://www.getnocms.com/2012/11/better-multi-language-template-better.html
$language = $this->cms_language();
$pattern = array();
$pattern[] = "/\{\{ if_language:$language \}\}(.*?)\{\{ elif_language:.*?\{\{ end_if }}/s";
$pattern[] = "/\{\{ if_language:$language \}\}(.*?)\{\{ else \}\}.*?\{\{ end_if }}/s";
$pattern[] = "/\{\{ if_language:$language \}\}(.*?)\{\{ end_if }}/s";
$pattern[] = "/\{\{ if_language:.*?\{\{ elif_language:$language \}\}(.*?)\{\{ elif_language:.*?\{\{ end_if }}/s";
$pattern[] = "/\{\{ if_language:.*?\{\{ elif_language:$language \}\}(.*?)\{\{ else \}\}.*?\{\{ end_if }}/s";
$pattern[] = "/\{\{ if_language:.*?\{\{ elif_language:$language \}\}(.*?)\{\{ end_if }}/s";
$pattern[] = "/\{\{ if_language:.*?\{\{ else \}\}(.*?)\{\{ end_if }}/s";
$replacement = '$1';
$value = preg_replace($pattern, $replacement, $value);
So far everything was so good. But then, I noticed a problem. For example, I write a text area or an input that contains any of those pattern. Something like:
$value = '<input value="{{ if_language:indonesia }} do_something {{ end_if }}" />';
I don’t want the input value to be replaced even if it is match with the regex.
Since I’ve use a lot of patterns here, how is the most effective way to avoid this?
You may try to add
} }
before your existing
preg_replaceto avoid this in most cases.See this demo.