Skip to content

Commit

Permalink
Merge pull request #1 from the-mars/master
Browse files Browse the repository at this point in the history
Add support for modifiers to remove leading zeros or convert to spaces
  • Loading branch information
alphp authored Mar 17, 2022
2 parents c894643 + 8c606b6 commit 4707884
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/php-8.1-strftime.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,41 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) :
'%x' => $intl_formatter,
];

$out = preg_replace_callback('/(?<!%)(%[a-zA-Z])/', function ($match) use ($translation_table, $timestamp) {
if ($match[1] == '%n') {
$out = preg_replace_callback('/(?<!%)%([_#-]?)([a-zA-Z])/', function ($match) use ($translation_table, $timestamp) {
$prefix = $match[1];
$char = $match[2];
$pattern = '%'.$char;
if ($pattern == '%n') {
return "\n";
}
elseif ($match[1] == '%t') {
elseif ($pattern == '%t') {
return "\t";
}

if (!isset($translation_table[$match[1]])) {
throw new InvalidArgumentException(sprintf('Format "%s" is unknown in time format', $match[1]));
if (!isset($translation_table[$pattern])) {
throw new InvalidArgumentException(sprintf('Format "%s" is unknown in time format', $pattern));
}

$replace = $translation_table[$match[1]];
$replace = $translation_table[$pattern];

if (is_string($replace)) {
return $timestamp->format($replace);
$result = $timestamp->format($replace);
}
else {
return $replace($timestamp, $match[1]);
$result = $replace($timestamp, $pattern);
}

switch ($prefix) {
case '_':
// replace leading zeros with spaces but keep last char if also zero
return preg_replace('/\G0(?=.)/', ' ', $result);
case '#':
case '-':
// remove leading zeros but keep last char if also zero
return preg_replace('/^0+(?=.)/', '', $result);
}

return $result;
}, $format);

$out = str_replace('%%', '%', $out);
Expand Down

0 comments on commit 4707884

Please sign in to comment.