Created
February 27, 2015 14:30
-
-
Save fuelingtheweb/a71a28545b0cf45f706c to your computer and use it in GitHub Desktop.
PHP Helpers
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 | |
if ( ! function_exists('parseString')) { | |
/** | |
* Parse a string and replace variables in the form of @(varName) | |
* with values of passed in variables in the form of $varName | |
* | |
* @param string $string | |
* @param array $data | |
* @param closure $callback | |
* @return string | |
*/ | |
function parseString($string, $data = [], $callback = null) { | |
return preg_replace_callback('/(@\(([A-Z]+)\))/i', function($matches) use($data, $callback) { | |
$match = $matches[2]; | |
if (!isset($data[$match])) return $matches[1]; | |
if (!is_null($callback)) return $callback($data[$match]); | |
return $data[$match]; | |
}, $string); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment