Forzare i leading zero su un numero o una stringa in PHP

code, php

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Per esempio se volete che la vostra stringa sia di 10 caratteri e che i caratteri aggiuntivi siano degli zero, potreste usare questo codice:

$valore = str_pad($valore, 10, 0);

che sfrutta questa funzione php:

string str_pad ( string $input , int $pad_length [, string $pad_string [, int $pad_type ]] )

This functions returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.

Che in pratica vuol dire:
La fuzione restituisce la stringa “$input” allungata a destra, a sinistra o da entrambi i lati fino ad arrivare alla lunghezza definita ($pad_lenght). Se non è definito il parametro $pad_string, la stringa viene allungata per mezzo di spazi.

permanent link - Comments Off

Prodotti correlati

:)

TAGS

***