this function never pads integers with leading zeros - help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    #1

    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_exis ts('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_CONS TANT, $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_CONS TANT) {
    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]

  • Joshie Surber

    #2
    Re: this function never pads integers with leading zeros - help

    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_exis ts('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_CONS TANT, $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_CONS TANT) {
    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]

    Comment

    • comp.lang.php

      #3
      Re: this function never pads integers with leading zeros - help


      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.prin tln(stuff.toStr ing());

      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_exis ts('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_CONS TANT, $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_CONS TANT) {
      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]

      Comment

      • Jerry Stuckle

        #4
        Re: this function never pads integers with leading zeros - help

        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.prin tln(stuff.toStr ing());
        >
        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.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • comp.lang.php

          #5
          Re: this function never pads integers with leading zeros - help


          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.prin tln(stuff.toStr ing());

          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 "inconsiste nt" in its
          explanation of str_pad() when it comes to this.

          Fixed my function and all is well:

          [PHP]
          if (!function_exis ts('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_CONS TANT, $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_CONS TANT) {
          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@attgl obal.net
          =============== ===

          Comment

          Working...