SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formatting a long string into a valid number format

    Say I have a string like this: "Points: 232134"

    I want to format it to "Points: 232,134"

    Unfortunately I can't figure out a way to strip out the Points part of the string from the number since the string is all bunched together.

    Any ideas?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    You could split it on the colon, to separate them in to different strings.

    Code php:
    $str = "Points: 232134";
    $list($label, $value) = explode(': ', $str);
    // do stuff with $value
    // ...
    // then
    $str = $label . ': ' . $value;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    var_dumppreg_replace_callback'/\d+/',
      function ( 
    $m ) {
        return 
    number_format$m );
      }, 
    'Points: 232134' ) ); 
    Last edited by paul_wilkins; Dec 8, 2010 at 23:29. Reason: preg_replace -> preg_replace_callback
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by logic_earth View Post
    PHP Code:
    var_dumppreg_replace_callback'/\d+/',
      function ( 
    $m ) {
        return 
    number_format$m );
      }, 
    'Points: 232134' ) ); 
    That's good - preg_replace_callback is a good technique where you have PHP 5.3 or better.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by logic_earth View Post
    PHP Code:
    var_dumppreg_replace_callback'/\d+/',
      function ( 
    $m ) {
        return 
    number_format$m );
      }, 
    'Points: 232134' ) ); 
    That's beautiful!

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Immerse View Post
    That's beautiful!
    Don't encourage him.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Hehe, maybe I should have written "Hmmm... interesting." instead, so that I don't appear to impressed

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ha, yes, I like that.

    Interesting Logic_Earth, just plain ol' interesting...
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

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