468,272 Members | 2,073 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

this function never pads integers with leading zeros - help

Could someone please spot-check this function and tell me why it never,
ever, pads an integer with a leading zero when I want it to?

This function fails in PHP 4.3.8, 4.4.1 and 4.3.9

Thanx
Phil

[PHP]
f (!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]

Sep 1 '06 #1
4 1943
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.
comp.lang.php wrote:
Could someone please spot-check this function and tell me why it never,
ever, pads an integer with a leading zero when I want it to?

This function fails in PHP 4.3.8, 4.4.1 and 4.3.9

Thanx
Phil

[PHP]
f (!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]
Sep 1 '06 #2

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
comp.lang.php wrote:
Could someone please spot-check this function and tell me why it never,
ever, pads an integer with a leading zero when I want it to?

This function fails in PHP 4.3.8, 4.4.1 and 4.3.9

Thanx
Phil

[PHP]
f (!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]
Sep 2 '06 #3
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);

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 2 '06 #4

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/viewt...=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.
js*******@attglobal.net
==================
Sep 2 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by r.magdeburg | last post: by
3 posts views Thread by alex.mcshane | last post: by
1 post views Thread by mmmgood1 | last post: by
5 posts views Thread by OneDay | last post: by
19 posts views Thread by Robbie Hatley | last post: by
6 posts views Thread by inexion | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.