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?

You could split it on the colon, to separate them in to different strings.


$str = "Points: 232134";
$list($label, $value) = explode(': ', $str);
// do stuff with $value
// ...
// then
$str = $label . ': ' . $value;


var_dump( preg_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.

That’s beautiful!

Don’t encourage him. :stuck_out_tongue:

Hehe, maybe I should have written “Hmmm… interesting.” instead, so that I don’t appear to impressed :slight_smile:

Ha, yes, I like that.

Interesting Logic_Earth, just plain ol’ interesting… :smiley: