Skip to content

Instantly share code, notes, and snippets.

@sebastianbergmann
Created January 11, 2022 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastianbergmann/e3a0bc5f821169edb4fd23eda7d0e295 to your computer and use it in GitHub Desktop.
Save sebastianbergmann/e3a0bc5f821169edb4fd23eda7d0e295 to your computer and use it in GitHub Desktop.
<?php declare(strict_types=1);
function mb_sprintf($format, ...$args): string
{
$params = $args;
return sprintf(
preg_replace_callback(
'/(?<=%|%-)\d+(?=s)/',
static function ($length) use (&$params) {
$value = array_shift($params);
return strlen($value) - mb_strlen($value) + $length[0];
},
$format
),
...$args
);
}
function mb_printf($format, ...$args): void
{
print mb_sprintf($format, ...$args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment