* @license GPLv2 http://files.syscp.org/misc/COPYING.txt * @package Functions * @version $Id: function.replace_variables.php 2724 2009-06-07 14:18:02Z flo $ */ /** * Replaces all occurences of variables defined in the second argument * in the first argument with their values. * * @param string The string that should be searched for variables * @param array The array containing the variables with their values * @return string The submitted string with the variables replaced. * @author Michael Duergner */ function replace_variables($text, $vars) { $pattern = "/\{([a-zA-Z0-9\-_]+)\}/"; // --- martin @ 08.08.2005 ------------------------------------------------------- // fixing usage of uninitialised variable $matches = array(); // ------------------------------------------------------------------------------- if(count($vars) > 0 && preg_match_all($pattern, $text, $matches)) { for ($i = 0;$i < count($matches[1]);$i++) { $current = $matches[1][$i]; if(isset($vars[$current])) { $var = $vars[$current]; $text = str_replace("{" . $current . "}", $var, $text); } } } $text = str_replace('\n', "\n", $text); return $text; }