Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#3 Please consider adding utf8_sprintf/utf8_vprintf

open
nobody
None
5
2007-09-29
2007-09-29
aprisobal
No

function utf8_sprintf($format) {
$argv = func_get_args();
array_shift($argv);
return utf8_vprintf($format, $argv);
}

function utf8_vprintf($format, $arguments) {
if (mb_internal_encoding() != 'UTF-8') {
return vsprintf($format, $arguments);
}
$newargv = array();

preg_match_all("`\%('.+|[0 ]|)([1-9][0-9]*|)s`U", $format, $results, PREG_SET_ORDER);
if (count($results)) {
foreach($results as $result) {
list($string_format, $filler, $size) = $result;
if(strlen($filler) > 1) $filler = substr($filler, 1);
while(count($arguments) && !is_string($arg = array_shift($arguments))) $newargv[] = $arg;
$pos = strpos($format, $string_format);
$format = substr($format, 0, $pos)
. ($size ? str_repeat($filler, $size-strlen($arg)) : '')
. str_replace('%', '%%', $arg)
. substr($format, $pos+strlen($string_format));
}
if (!count($newargv)) {
return $format;
} else {
return vsprintf($format, $newargv);
}
} else {
return vsprintf($format, $arguments);
}
}

Discussion