Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Insert commas

  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post

    Insert commas

    I try to do my own research before posting. I have a variable that can contain a large string of numbers. I would like commas inserted where needed to make it easier to read.

    I found this:

    PHP Code:
    <?php 
    $numbers 
    implode("",range(1,15)); 
    print 
    "$numbers"//123456789101112131415
    //now insert commas 
    $newnumbers substr(chunk_split($numbers,3,","),0,-1); 
    print 
    "$newnumbers"
    //outputs: 123,456,789,101,121,314,15
    ?>
    As you see this works left to right... not right to left.

    What is the proper solution?

  • #2
    New to the CF scene
    Join Date
    Jan 2009
    Location
    Elkton, MD
    Posts
    9
    Thanks
    0
    Thanked 2 Times in 2 Posts

    number_format()

    http://us.php.net/number_format

    PHP has a built-in function for inserting decimals and commas. Good luck!

    Ranjit Viswakumar
    Professional Services Specialist
    HostMySite.com

  • Users who have thanked TapeBoy929 for this post:

    Hayyel (03-04-2009)

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post
    Just what I was looking for. Thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •