how split number 3 parts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jason7899
    New Member
    • Mar 2008
    • 59

    #1

    how split number 3 parts

    hi,
    i have a number with 9 digits lenght
    and i want split this number: 922888222
    into this: 922 888 222
    how can i do that?
    thanks a lot for your help :)
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you can use a RegEx (preg_replace()).

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by Dormilich
      you can use a RegEx (preg_replace()).
      preg_split() would be better ;)

      And str_split() better still - pay attention to the optional 'length' parameter.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by Markus
        preg_split() would be better
        that depends on the desired output.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by Dormilich
          that depends on the desired output.
          I don't think the output is in question - the OP asked for his string to be split.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            the OP didn’t mention to split it into an array. you know how loose the meaning of a word can be in this forum.

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              I am presuming that it is a phone number or the like that the OP wants formatted. How I would do it, not saying Dorms way won't work or be better, but str_split() and then a simple echo with spaces. It can be done in a loop if the length of the original number is variable, but I think this will do:
              Code:
              $number = "922888222";
              $array = str_split($number,3); // Splits the number into a new element every 3 characters
              echo( $array[0],' ', $array[1],' ', $array[2] ); // Ouputs each element with a space in between

              Comment

              • zorgi
                Recognized Expert Contributor
                • Mar 2008
                • 431

                #8
                I would probably do it like this


                Code:
                number_format(922888222, 0, " ", " ");

                Comment

                • TheServant
                  Recognized Expert Top Contributor
                  • Feb 2008
                  • 1168

                  #9
                  Originally posted by zorgi
                  I would probably do it like this


                  Code:
                  number_format(922888222, 0, " ", " ");
                  http://hr.php.net/number_format
                  Great idea! Looks like it would work.

                  Comment

                  • eladkarako
                    New Member
                    • May 2007
                    • 2

                    #10
                    Code:
                    /**
                     * @param float $size                - the memory size to format
                     * @param bool  $is_add_commas       (optional) - saperete every 3 digits from the end so 56789 will be "56,789".
                     * @param bool  $is_full_description (optional) - use *Bytes instead of *b (GigaBytes instead of gb, etc...).
                     * @param int   $digits              (optional) - number of digits decimal point, to limit.
                     * @return string
                     * @author http://icompile.eladkarako.com
                     */
                    function human_readable_memory_sizes($size, $is_add_commas = false, $is_full_description = false, $digits = 20) {
                      $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
                      $unit_full = array('Bytes', 'KiloByte', 'MegaBytes', 'GigaBytes', 'TeraBytes', 'PetaBytes');
                    
                      $out = $size / pow(1024, ($i = floor(log($size, 1024))));
                    
                      $out = sprintf("%." . $digits . "f", $out);
                    
                      $out = !$is_add_commas ? $out : preg_replace_callback('/(\d)(?=(\d{3})+$)/', function ($arr) {
                        return isset($arr[0]) ? "$arr[0]," : "";
                      }, $out);
                    
                      $out .= ' ' . (!$is_full_description ? $unit[ $i ] : $unit_full[ $i ]);
                    
                      return $out;
                    }
                    the preg_replace_ca llback is what you want (in PHP),
                    for JavaScript your can use:
                    Code:
                    function addCommas(t) {
                      return String(t).replace(/(\d)(?=(\d{3})+$)/g, "$1,")
                    }

                    taken from:
                    first one:


                    second one:


                    Elad.

                    Comment

                    Working...