I have two ways to create masks for numbers in PHP, however I do not know if it is the most elegant and effective, so I would like comments to improve the code.
I need the function to behave as follows:
- Format numbers with less than 8 numbers by filling them with
zero
. - Create a separator (it can be a hyphen) in the middle of these 8 numbers. That is,
11112222
will transform to1111-2222
.
I already have these two examples:
Example 1:
implode('-', str_split(sprintf('%08s', $numero), 4));
Example 2
$formatado = sprintf('%08s', $numero);
substr($formatado, 0, 4) . '-' . substr($formatado, 4, 8);
Does anyone know a better way, in terms of performance and elegance, without having to call various functions to create a mask in PHP?