src/Str.php
public static function nsprintf($format, array $args = [])
    {
        // Filter unnamed %s strings that should not be processed
        $filterRegex = '/%s/';
        $format = preg_replace($filterRegex, '#[:~s]#', $format);

        // The pattern to match variables
        $pattern = '/(?<=%)([a-zA-Z0-9_]\w*)(?=\$)/';

        // Add predefined values
        $pool = ['cr' => "\r", 'lf' => "\n", 'crlf' => "\r\n", 'tab' => "\t"] + $args;

        // Build args array and substitute variables with numbers
        $args = [];
        $pos = 0;
        $match = null;
        while (static::match($format, $pattern, $match, PREG_OFFSET_CAPTURE, $pos)) {
            list($varKey, $varPos) = $match[0];

            if (!array_key_exists($varKey, $pool)) {
                throw new \BadFunctionCallException("Missing argument '${varKey}'.", E_USER_WARNING);
            }

            array_push($args, $pool[$varKey]);
            $format = substr_replace($format, count($args), $varPos, $wordLength = static::length($varKey));
            $pos = $varPos + $wordLength; // skip to end of replacement for next iteration
        }

        // Return the original %s strings
        $filterRegex = '/#\[:~s\]#/';

        return preg_replace($filterRegex, '%s', vsprintf($format, $args));
    }
public static function nsprintf($format, array $args = [])
    {
        // Filter unnamed %s strings that should not be processed
        $filterRegex = '/%s/';
        $format = preg_replace($filterRegex, '#[:~s]#', $format);

        // The pattern to match variables
        $pattern = '/(?<=%)([a-zA-Z0-9_]\w*)(?=\$)/';

        // Add predefined values
        $pool = ['cr' => "\r", 'lf' => "\n", 'crlf' => "\r\n", 'tab' => "\t"] + $args;

        // Build args array and substitute variables with numbers
        $args = [];
        $pos = 0;
        $match = null;
        while (static::match($format, $pattern, $match, PREG_OFFSET_CAPTURE, $pos)) {
            list($varKey, $varPos) = $match[0];

            if (!array_key_exists($varKey, $pool)) {
                throw new \BadFunctionCallException("Missing argument '${varKey}'.", E_USER_WARNING);
            }

            array_push($args, $pool[$varKey]);
            $format = substr_replace($format, count($args), $varPos, $wordLength = static::length($varKey));
            $pos = $varPos + $wordLength; // skip to end of replacement for next iteration
        }

        // Return the original %s strings
        $filterRegex = '/#\[:~s\]#/';

        return preg_replace($filterRegex, '%s', vsprintf($format, $args));
    }
public static function nsprintf ( $ format , array $ args = [ ] ) { // Filter unnamed %s strings that should not be processed $ filterRegex = '/%s/' ; $ format = preg_replace ( $ filterRegex , '#[:~s]#' , $ format ) ; // The pattern to match variables $ pattern = '/(?<=%)([a-zA-Z0-9_]\w*)(?=\$)/' ; // Add predefined values $ pool = [ 'cr' => "\r" , 'lf' => "\n" , 'crlf' => "\r\n" , 'tab' => "\t" ] + $ args ; // Build args array and substitute variables with numbers $ args = [ ] ; $ pos = 0 ; $ match = null ; while ( static :: match ( $ format , $ pattern , $ match , PREG_OFFSET_CAPTURE , $ pos ) ) { list ( $ varKey , $ varPos ) = $ match [ 0 ] ; if ( ! array_key_exists ( $ varKey , $ pool ) ) { throw new \ BadFunctionCallException ( "Missing argument '${varKey}'." , E_USER_WARNING ) ; } array_push ( $ args , $ pool [ $ varKey ] ) ; $ format = substr_replace ( $ format , count ( $ args ) , $ varPos , $ wordLength = static :: length ( $ varKey ) ) ; $ pos = $ varPos + $ wordLength ; // skip to end of replacement for next iteration } // Return the original %s strings $ filterRegex = '/#\[:~s\]#/' ; return preg_replace ( $ filterRegex , '%s' , vsprintf ( $ format , $ args ) ) ; }
Function sprintf for named variables inside format.

Args are only used if found in the format.
Predefined tags (which can be overwritten passing them as args):
%cr$s -> \r
%lf$s -> \n
%crlf$s -> \r\n
%tab$s -> \t

This method is based on the work of Nate Bessette (www.twitter.com/frickenate)

@param string $format The format to replace the named variables into
@param array  $args   The args to be replaced (var => replacement).

@return string|bool  The string with args replaced or false on error
@throws \BadFunctionCallException
@throws \LengthException
Function sprintf for named variables inside format .