trim(...) |> strtolower(...) |> (static fn (string $text): string => str_replace(' ', '-', $text)); if ($value === '') { throw new InvalidArgumentException('Identifier must not be empty'); } return $value; } foreach ([' RELEASE NOTES ', 'TWO SPACES', ' ', 'café'] as $input) { try { echo identifier($input), PHP_EOL; } catch (InvalidArgumentException $error) { echo $error->getMessage(), PHP_EOL; } }