Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
Function to Swap words inside double curly braces for entries in an array
function curlySwap($shortcodes,$string) {
return preg_replace_callback(
function ($match) {
global $shortcodes;
return $shortcodes[substr($match[0], 2, -2)];
$shortcodes = array(
'email' => '',
'tel' => '07912 345678'
$string = 'My email is {{email}} and telephone {{tel}}.';
echo curlySwap($shortcodes,$string);
// Produces
// My email is and telephone 07912 345678.

This comment has been minimized.

Copy link

@rbk 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