Skip to content

Instantly share code, notes, and snippets.

@fuelingtheweb
Created February 27, 2015 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuelingtheweb/a71a28545b0cf45f706c to your computer and use it in GitHub Desktop.
Save fuelingtheweb/a71a28545b0cf45f706c to your computer and use it in GitHub Desktop.
PHP Helpers
<?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