472,965 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,965 software developers and data experts.

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 2149
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: r.magdeburg | last post by:
//please tell me why... //and give me a hint to solve the problem with leading zeros. //snippet #include <iostream.h> #include <conio.h> int main() { int zahl = 0; cout << "Give me an int...
7
by: david | last post by:
Hi, I have 2 text boxes on an ASP form. A user enters a Serial Number in TB1 such as 0105123456, presses tab to move to TB2, TB2 then displays the value of TB1 after a calculation has been...
3
by: alex.mcshane | last post by:
Hi - I would be grateful for any advice on the following. Within DB2 for OS/390, the STRIP Scaler Function is available. Its function is, for example, to remove leading zeros from a string. ...
1
by: mmmgood1 | last post by:
Help, I'm linking an excel spreadsheet in access and I have datafields with leading zeros (01021). When the file is linked in access, I get a #num in the field with the leading zeros. The zeros...
5
by: OneDay | last post by:
I've got a field that has some old data with text in it, but all forward data will be a 3 digit number. But many of the numbers are still only 2 digits. I would like to force the leading zero in...
19
by: Robbie Hatley | last post by:
For your enjoyment, a function that expresses any integer with absolute value less-than-or-equal-to nine quintillion in any base from 2 to 36. (For larger bases, you could expand the "digit"...
3
by: ineedahelp | last post by:
Can anyone help me...I am trying to output my access report to excel. One of my fields is formatted as text, but excel is dropping leading zeros...only on the records where all values are numeric. ...
11
by: ram23 | last post by:
Hi friends, i need help .. i am getting String value and i need to add leading zeros.. total length of Target string is 13char eg: suppose if i get 12 as values.. it needs to convert into...
6
by: inexion | last post by:
hello, can anyone help me out with a bit handler? it seems as if php's decbin() removes leading zeroes when converting from hex....so for example decbin(hexdec()); basically removes any...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.