-
-
Save iNamik/4147047 to your computer and use it in GitHub Desktop.
Hello,
If possible, could you please provide an example using the above code.
Thank you.
Hi @CasperUK,
Have a look at this example:
$map = array( "greeting" => "Hello", "name" => "Newman");
#
# Test with case-matching keys
#
$template = "{{greeting}}, {{name}}";
# output (working):
# Hello, Newman
echo replace_tags($template, $map);
echo "\r\n";
#
# Test with case-mismatched keys
#
$template = "{{Greeting}}, {{NAME}}";
# output (missing values):
# ,
echo replace_tags($template, $map);
echo "\r\n";
#
# Test with case-mismatched keys but $force_lower = true
#
$template = "{{Greeting}}, {{NAME}}";
# output (working):
# Hello, Newman
echo replace_tags($template, $map, true);
echo "\r\n";
Hope that helps and thank you for your interest in my Gist !
This gist is working like a charm!! Thank ya for sharing
This gist is working like a charm!! Thank ya for sharing
Thanks ! That means a lot for code I created in 2017 :)
The current regexp doesn't account for trailing or leading whitespace in the string. Eg. {{ foo }}
will require an input of [' foo ' => 'bar']
in order to work - We needed an implementation that allows for that without causing issues.
A regexp that accounts for that could look like the following: '/{{\s*([^{}]+\S)\s*}}/'
-> https://regex101.com/r/4ts83c/1
I'd also recommend using mb_strtolower
if possible, if you have even the slightest chance of encountering multibyte characters (æ, ø, å, ü etc) when lower casing.
Great code otherwise! Thanks!
@ExeQue wrote:
A regexp that accounts for that could look like the following:
'/{{\s*([^{}]+\S)\s*}}/'
-> https://regex101.com/r/4ts83c/1
Thanks for the suggestion - I've updated the regex but with a slightly different take - My update doesn't expect or allow \s
to exist in the tag name.
I'd also recommend using
mb_strtolower
if possible, if you have even the slightest chance of encountering multibyte characters (æ, ø, å, ü etc) when lower casing.
Thats good advice, but I think there's a general consensus that the mb_
functions are slower, so I decided to just add a comment for the user to decide if the change makes sense for their use case.
Great code otherwise! Thanks!
Thanks! Its fun to see this gist still getting used so long after I created it !
Hello,
If possible, could you please provide an example using the above code.
Thank you.