Voting

: three minus one?
(Example: nine)

The Note You're Voting On

Bradley from California
19 years ago
Add on to (a function originally written by) "Matias from Argentina": str_format_number function.

Just added handling of $String shorter then $Format by adding a side to start the fill and a string length to the while loop.

<?php
function str_format_number($String, $Format, $Start = 'left'){
//If we want to fill from right to left incase string is shorter then format
if ($Start == 'right') {
$String = strrev($String);
$Format = strrev($Format);
}
if(
$Format == '') return $String;
if(
$String == '') return $String;
$Result = '';
$FormatPos = 0;
$StringPos = 0;
while ((
strlen($Format) - 1) >= $FormatPos && strlen($String) > $StringPos) {
//If its a number => stores it
if (is_numeric(substr($Format, $FormatPos, 1))) {
$Result .= substr($String, $StringPos, 1);
$StringPos++;
//If it is not a number => stores the caracter
} else {
$Result .= substr($Format, $FormatPos, 1);
}
//Next caracter at the mask.
$FormatPos++;
}
if (
$Start == 'right') $Result = strrev($Result);
return
$Result;
}
?>

<< Back to user notes page

To Top