5485 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Located in Australia
  • Has sold $1,000+ on Envato Market
+4 more
Australia says

So lets say we have a string whos value could be

1,234 or

12,567 or

1,123,456 or

17,865,345,198

Ok so I can figure out Millions.

<?php
$value = '123,234,593';
$pattern  = '/,\d{3},\d{3}$/i';
$newvalue = ' Million';
echo preg_replace($pattern , $newvalue, $value);
?>

But I want to echo other value denominators too, so if the value = 123,456 I would like to echo 123 Thousand

if it is 1,234,567,123 I would like to echo 1.24 Billion

etc.

438 posts here for love
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 1+ items on Envato Market
+7 more
nCrafts says

If I am not wrong, you want to convert a currency amount to words. Use substr_count to find the number of commas in the amount. One comma = thousand Two commas = millions Three commas = billions Use case switch to set the appropriate value here. Let this be x.
Next use strpos to find the number of characters to the left of the first instance of a comma in the amount. Let this be y. Now use another function to get the characters between the first and second instance of a comma. Let this be z.

Your final answer should be something like = $x.$y.$z

5485 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Located in Australia
  • Has sold $1,000+ on Envato Market
+4 more
Australia says

Thanks, but not what I am after :)

639 posts
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
+1 more
Crakken says
Would look like something like that:
<?php
$value = '176,987';

$thousand =  (substr($value, 0, -4) + 1)."k";

echo $thousand;

?>

I can make you a complete function including million and billion if you want.

5485 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Located in Australia
  • Has sold $1,000+ on Envato Market
+4 more
Australia says

Thanks Sushi ( http://peter-ajtai.com/examples/numbers.php ) Really long winded but kind of thing, just a tad too heavy weight for my liking.

Crakken I will have a play with that thankyou

639 posts
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
+1 more
Crakken says

Well, there you go: http://snippi.com/s/98pab35
In case you don’t have the original value with commas, use number_format.

5485 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Located in Australia
  • Has sold $1,000+ on Envato Market
+4 more
Australia says

Well, there you go: http://snippi.com/s/98pab35
In case you don’t have the original value with commas, use number_format.

Awesome, thanks for doing that. Just what I was after.

Well … will play with number format, becuase I would like to show to nearest cpl of dec places ( kind of thang )

Like 1.78 Billion etc

Seriously great , appreciate that

639 posts
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
+1 more
Crakken says

Well … will play with number format, becuase I would like to show to nearest cpl of dec places ( kind of thang )

Lol, I couldn’t understand that part, but no problem.

159 posts
  • Has sold $100+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
  • Has been part of the Envato Community for over 2 years
+1 more
s4nji says

Here, I made one with preg_match_all, pretty much worked :)

5485 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Located in Australia
  • Has sold $1,000+ on Envato Market
+4 more
Australia says

Here, I made one with preg_match_all, pretty much worked :)

Sanji thats pretty awesome work, may i use your code too. thankyou :)

by
by
by
by
by
by