I wish you had explained it, but your solution works.. it sppears it works because str_pad() has a fundamental flaw that refuses to pad a string if the offset is the same length as the string itself. My pad() function now works perfectly, so thanx!
if (!function_exists('pad')) {
/**
* Pad a string. Use str_pad() as default unless the input string value is larger than the amount of padding to take place due to str_pad() rules (PHP needs to update this!!)
*
* @access public
* @param mixed $input (reference)
* @param int $offset Number of times to repeat padding character
* @param char $padChar Character to pad with
* @param int $padConstant (optional) PAD_CONSTANT constant value (e.g. STR_PAD_LEFT, STR_PAD_RIGHT, etc.)
* @return mixed $paddedString
* @link http://us3.php.net/manual/en/function.str-pad.php
* @see link regarding usage of str_pad() function
*/
function &pad(&$input, $offset, $padChar, $padConstant = STR_PAD_RIGHT) {
define(PAD_CONSTANT, $padConstant); // MAKE INTO CONSTANT FOR NAMING CONVENTION
if ((int)$offset > 0 && (int)$offset > strlen($input)) return str_pad($input, $offset, $padChar, PAD_CONSTANT); // USE str_pad() BY DEFAULT IF RULES APPLY
if ((int)$offset === 0 || strlen($input) == 0 || !isset($padChar) || strlen($padChar) < 1) return $input; // NOTHING TO PAD
switch(PAD_CONSTANT) {
case STR_PAD_LEFT:
for ($i = 1; $i <= $offset; $i++) $input = "$padChar$input";
break;
case STR_PAD_RIGHT:
for ($i = 1; $i <= $offset; $i++) $input = "$input$padChar";
break;
case STR_PAD_BOTH:
for ($i = 1; $i <= $offset; $i++) $input = "$padChar$input$padChar";
break;
default: // DO NOTHING
break;
}
return $input;
}
}
No grade-school math knowledge required.
Phil