how split number 3 parts

Discussion in 'PHP' started by srdva59, Sep 30, 2009.

  1. #1
    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 :)
     
    srdva59, Sep 30, 2009 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    <?
    $str="922888222";
    $str1=substr($str, 0, 3);
    $str2= substr($str, 3, 3);
    $str3= substr($str, 6, 3);
    echo str1;
    echo str2;
    echo str3;

    ?>
    I hope it helps
    Regards

    Alex
     
    Last edited: Sep 30, 2009
    kmap, Sep 30, 2009 IP
  3. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #3
    There is also a built-in PHP function that does it

    
    $string = "922888222";
    $split = str_split($string, 3);
    
    /*
    $split[0] = "922";
    $split[1] = "888";
    $split[2] = "222";
    */
    
    PHP:
     
    Gray Fox, Sep 30, 2009 IP
  4. sudeep333

    sudeep333 Peon

    Messages:
    321
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Both codes will work but i will prefer the second one should be used because it will use lesser memory of the PHP processor.
     
    sudeep333, Oct 1, 2009 IP