* * * This will output "There are 5 monkeys in the tree". But * imagine we are creating a format string in a separate file, * commonly because we would like to internationalize it and we * rewrite it as: * * Argument swapping * * * ]]> * * * We now have a problem. The order of the placeholders in the * format string does not match the order of the arguments in the * code. We would like to leave the code as is and simply indicate * in the format string which arguments the placeholders refer to. * We would write the format string like this instead: * * Argument swapping * * * ]]> * * * An added benefit here is that you can repeat the placeholders without * adding more arguments in the code. For example: * * Argument swapping * * * ]]> * * * When using argument swapping, the n$ * position specifier must come immediately * after the percent sign (%), before any other * specifiers, as shown in the example below. * * Specifying padding character * * * ]]> * * The above example will output: * * * * * * Position specifier with other specifiers * * * ]]> * * The above example will output: * * * * * * The above example will output: * * The above example will output: * * Attempting to use a position specifier greater than * PHP_INT_MAX will result in * sprintf generating warnings. * * The c type specifier ignores padding and width * @param mixed $params * @return string Returns a string produced according to the formatting string * format. * @throws StringsException * */ function sprintf(string $format, ...$params): string { error_clear_last(); if ($params !== []) { $result = \sprintf($format, ...$params); } else { $result = \sprintf($format); } if ($result === false) { throw StringsException::createFromPhpError(); } return $result; } /** * Returns the portion of string specified by the * start and length parameters. * * @param string $string The input string. Must be one character or longer. * @param int $start If start is non-negative, the returned string * will start at the start'th position in * string, counting from zero. For instance, * in the string 'abcdef', the character at * position 0 is 'a', the * character at position 2 is * 'c', and so forth. * * If start is negative, the returned string * will start at the start'th character * from the end of string. * * If string is less than * start characters long, FALSE will be returned. * * * Using a negative start * * * ]]> * * * @param int $length If length is given and is positive, the string * returned will contain at most length characters * beginning from start (depending on the length of * string). * * If length is given and is negative, then that many * characters will be omitted from the end of string * (after the start position has been calculated when a * start is negative). If * start denotes the position of this truncation or * beyond, FALSE will be returned. * * If length is given and is 0, * FALSE or NULL, an empty string will be returned. * * If length is omitted, the substring starting from * start until the end of the string will be * returned. * @return string Returns the extracted part of string;, or * an empty string. * @throws StringsException * */ function substr(string $string, int $start, int $length = null): string { error_clear_last(); if ($length !== null) { $result = \substr($string, $start, $length); } else { $result = \substr($string, $start); } if ($result === false) { throw StringsException::createFromPhpError(); } return $result; }