Skip to content

Instantly share code, notes, and snippets.

@JonCatmull

JonCatmull/curly_swap.php

Last active Aug 29, 2015
Embed
What would you like to do?
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

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