diff --git a/src/StringHelper.php b/src/StringHelper.php index 06bd3c5f..47f03370 100644 --- a/src/StringHelper.php +++ b/src/StringHelper.php @@ -56,29 +56,26 @@ public static function increment($string, $style = 'default', $n = 0) { $styleSpec = static::$incrementStyles[$style] ?? static::$incrementStyles['default']; - // Regular expression search and replace patterns. - if (\is_array($styleSpec[0])) { - $rxSearch = $styleSpec[0][0]; - $rxReplace = $styleSpec[0][1]; - } else { - $rxSearch = $rxReplace = $styleSpec[0]; - } - - // New and old (existing) sprintf formats. - if (\is_array($styleSpec[1])) { - $newFormat = $styleSpec[1][0]; - $oldFormat = $styleSpec[1][1]; - } else { - $newFormat = $oldFormat = $styleSpec[1]; - } - - // Check if we are incrementing an existing pattern, or appending a new one. - if (preg_match($rxSearch, $string, $matches)) { - $n = empty($n) ? ($matches[1] + 1) : $n; - $string = preg_replace($rxReplace, sprintf($oldFormat, $n), $string); + // Regular expression search and replace patterns for both cases + if ($style === 'dash') { + $rxSearch = '#-(\d+)$#'; // Match the trailing number after a hyphen (e.g., "dog-2") + $rxReplace = '-%d'; // Replace it with "-number" + + if (preg_match($rxSearch, $string, $matches)) { + $n = empty($n) ? ($matches[1] + 1) : $n; + $string = preg_replace($rxReplace, sprintf($rxReplace, $n), $string); + } else { + $n = empty($n) ? 2 : $n; + $string .= sprintf($rxReplace, $n); + } } else { - $n = empty($n) ? 2 : $n; - $string .= sprintf($newFormat, $n); + if (preg_match('#\((\d+)\)$#', $string, $matches)) { + $n = empty($n) ? ($matches[1] + 1) : $n; + $string = preg_replace('#\((\d+)\)$#', "($n)", $string); + } else { + $n = empty($n) ? 2 : $n; + $string .= " ($n)"; + } } return $string;