Created
July 6, 2016 17:10
-
-
Save ssi-anik/b39249b9f1837d91423aed4a4fd71fe9 to your computer and use it in GitHub Desktop.
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 | |
| $template =[ | |
| '@name@' => [ | |
| 'User name', | |
| 'Anik', | |
| ], | |
| '@email@' => [ | |
| 'Email', | |
| 'my.mail@mail.com' | |
| ], | |
| '@address@' => [ | |
| 'Address', | |
| [ | |
| 'Diamond Head', | |
| 'Honolulu', | |
| 'Hawaii' | |
| ], | |
| ], | |
| ]; | |
| $text = "@name@ has an email of: @email@. He lives in @address@"; | |
| function replacer($match){ | |
| global $template; | |
| if(array_key_exists($match[0], $template)){ | |
| $value_on_match = $template[$match[0]]; | |
| if(is_string($value_on_match[1])){ | |
| return $value_on_match[1]; | |
| } elseif(is_array($value_on_match[1])){ | |
| return implode(", ", $value_on_match[1]); | |
| } | |
| } | |
| return $match[0]; | |
| } | |
| $pattern = sprintf("~%s~", implode("|", array_keys($template))); | |
| echo preg_replace_callback($pattern, "replacer", $text) . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment