Skip to content

Instantly share code, notes, and snippets.

@JonCatmull
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonCatmull/d35ab6043c843cd357c5 to your computer and use it in GitHub Desktop.
Save JonCatmull/d35ab6043c843cd357c5 to your computer and use it in GitHub Desktop.
Function to Swap words inside double curly braces for entries in an array
<?php
function curlySwap($shortcodes,$string) {
return preg_replace_callback(
'/\{\{(\w+)\}\}/',
function ($match) {
global $shortcodes;
return $shortcodes[substr($match[0], 2, -2)];
},
$string
);
}
$shortcodes = array(
'email' => 'jonathancatmull@gmail.com',
'tel' => '07912 345678'
);
$string = 'My email is {{email}} and telephone {{tel}}.';
echo curlySwap($shortcodes,$string);
// Produces
// My email is jonanthancatmull@gmail.com and telephone 07912 345678.
?>
@rbk
Copy link

rbk commented Jul 14, 2015

Awesome function!

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