Created
July 23, 2017 08:27
-
-
Save mhmoudsami/fe8e54a5d54f477a4eed012cb253412e to your computer and use it in GitHub Desktop.
php template
This file contains hidden or 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 string tempate | |
| * @param $string string | |
| * @param replacement_array array of key value pair to replace | |
| * @example render_string_template("my name is {name}" , ['name' => 'mahmoud sami']) | |
| */ | |
| if (! function_exists('render_string_template')) : | |
| function render_string_template($string , $replacement_array) | |
| { | |
| $string_processed = preg_replace_callback( | |
| '~\{\$(.*?)\}~si', | |
| function($match) use ($replacement_array) | |
| { | |
| return str_replace($match[0], isset($replacement_array[$match[1]]) ? $replacement_array[$match[1]] : $match[0], $match[0]); | |
| }, | |
| $string); | |
| return $string_processed; | |
| } | |
| endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment