Last active
August 29, 2015 14:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' => '[email protected]', | |
'tel' => '07912 345678' | |
); | |
$string = 'My email is {{email}} and telephone {{tel}}.'; | |
echo curlySwap($shortcodes,$string); | |
// Produces | |
// My email is [email protected] and telephone 07912 345678. | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome function!