Back to Text class

Method sprintf

public static string
sprintf
(mixed $string)
Passes a string thru a sprintf.
Parameters
  • string $string The format string.
Returns
  • string The translated strings or the key if 'script' is true in the array of options.
Since
  • 1.7.0
Class: Text
Project: Joomla

Method sprintf - Source code

/**
 * Passes a string thru a sprintf.
 *
 * Note that this method can take a mixed number of arguments as for the sprintf function.
 *
 * The last argument can take an array of options:
 *
 * array('jsSafe'=>boolean, 'interpretBackSlashes'=>boolean, 'script'=>boolean)
 *
 * where:
 *
 * jsSafe is a boolean to generate a javascript safe strings.
 * interpretBackSlashes is a boolean to interpret backslashes \\->\, \n->new line, \t->tabulation.
 * script is a boolean to indicate that the string will be push in the javascript language store.
 *
 * @param   string  $string  The format string.
 *
 * @return  string  The translated strings or the key if 'script' is true in the array of options.
 *
 * @since   1.7.0
 */
public static function sprintf($string)
{
    $lang = Factory::getLanguage();
    $args = \func_get_args();
    $count = \count($args);
    if (\is_array($args[$count - 1])) {
        $args[0] = $lang->_($string, \array_key_exists('jsSafe', $args[$count - 1]) ? $args[$count - 1]['jsSafe'] : false, \array_key_exists('interpretBackSlashes', $args[$count - 1]) ? $args[$count - 1]['interpretBackSlashes'] : true);
        if (\array_key_exists('script', $args[$count - 1]) && $args[$count - 1]['script']) {
            static::$strings[$string] = \call_user_func_array('sprintf', $args);
            return $string;
        }
    } else {
        $args[0] = $lang->_($string);
    }
    // Replace custom named placeholders with sprintf style placeholders
    $args[0] = preg_replace('/\\[\\[%([0-9]+):[^\\]]*\\]\\]/', '%\\1$s', $args[0]);
    return \call_user_func_array('sprintf', $args);
}