473,394 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2159
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.