Skip to content

Instantly share code, notes, and snippets.

@Stichoza
Last active December 14, 2015 07:09
Show Gist options
  • Save Stichoza/5048086 to your computer and use it in GitHub Desktop.
Save Stichoza/5048086 to your computer and use it in GitHub Desktop.
Easily replace variable-like tags in string using associative array's keys and values.
<?php
# usage: $array = array("name" => "Stichoza", "website" => "stichoza.com");
# echo replace_identifiers("Visit {{name}}'s website at {{website}}", $array);
function replace_identifiers($string, $assoc_array) {
foreach ($assoc_array as $key => $value) {
$string = str_replace("{{".$key."}}", $value, $string);
}
return $string;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment