public static function replaceArgs($str, $args, $escapeFunc = null)
    {
        if ($escapeFunc === null) {
            $escapeFunc = function ($x) {
                return $x;
            };
        }

        if (!is_callable($escapeFunc)) {
            throw new TextException("Escape function is not callable");
        }

        return preg_replace_callback(
            '/(^|[^\\\]){\w+}/',
            function ($match) use (&$args, $escapeFunc) {
                return $match[1] . call_user_func(
                    $escapeFunc, (count($args) > 0? array_shift($args) : $match[0])
                );
            },
            $str
        );
    }