Last active
December 15, 2015 03:29
-
-
Save l4ci/5195004 to your computer and use it in GitHub Desktop.
sprintf3 - Replace %var% from a string #php
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 | |
/** | |
* 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