Reply to Re: this function never pads integers with leading zeros - help

Your name:

Reply:


Posted by comp.lang.php on 09/02/06 05:16

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]

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация