|
Posted by comp.lang.php on 09/02/06 22:22
Jerry Stuckle wrote:
> comp.lang.php wrote:
> > Joshie Surber wrote:
> >
> >>Have you tried forcing your INT into a STRING, or making sure it is a
> >>string? If it is being treated as an INT padding is automatically
> >>disregarded.
> >
> >
> > How do you force an int into a string? In Java it would be:
> >
> > int stuff = 8;
> > System.out.println(stuff.toString());
> >
> > Or something like that
> >
> > Phil
> >
> >
>
> First of all, this is Java, not PHP. Two different languages which have
> to be handled two different ways - don' t confuse the two. And BTW -
> your example won't work for Java, either (no toString method for an int).
>
> As to converting to a string - you can so something like:
>
> $str = sprintf('%d', $var);
>
> --
According to this thread,
http://www.nordicfest.no/forum/viewtopic.php?t=2470&start=15 it appears
that the problem all along is with str_pad(), not the
integer-vs-string-vs-whatever issue that came up with a side thought.
It appears even the PHP Manual might be "inconsistent" in its
explanation of str_pad() when it comes to this.
Fixed my function and all is well:
[PHP]
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;
}
}
[/PHP]
Thanx
Phil
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstucklex@attglobal.net
> ==================
Navigation:
[Reply to this message]
|