Skip to content

Instantly share code, notes, and snippets.

@l4ci
Last active December 15, 2015 03:29
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 l4ci/5195004 to your computer and use it in GitHub Desktop.
Save l4ci/5195004 to your computer and use it in GitHub Desktop.
sprintf3 - Replace %var% from a string #php
<?php
/**
* replaces %your_var% from a string
*
* sprintf3( 'Hello %your_name%, my name is %my_name%!'
* , array( 'your_name' => 'Matt'
* , 'my_name' => 'Jim'
* )
* );
*/
function sprintf3($str, $vars, $char = '%'){
$tmp = array();
foreach($vars as $k => $v){
$tmp[$char . $k . $char] = $v;
}
return str_replace(array_keys($tmp), array_values($tmp), $str);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment