ThemeForest

php preg_replace

3386 posts
  • Australia
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Interviewed on the Envato Notes blog
  • Microlancer Beta Tester
  • Sold between 1 000 and 5 000 dollars
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.

208 posts
  • Has been a member for 0-1 years
  • Exclusive Author
  • Microlancer Beta Tester
  • Sold between 5 000 and 10 000 dollars
  • India
  • Bought between 1 and 9 items
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

3386 posts
  • Australia
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Interviewed on the Envato Notes blog
  • Microlancer Beta Tester
  • Sold between 1 000 and 5 000 dollars
Australia says

Thanks, but not what I am after :)

624 posts
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Bought between 10 and 49 items
  • Has been a member for 0-1 years
  • Haiti
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.

3386 posts
  • Australia
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Interviewed on the Envato Notes blog
  • Microlancer Beta Tester
  • Sold between 1 000 and 5 000 dollars
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

624 posts
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Bought between 10 and 49 items
  • Has been a member for 0-1 years
  • Haiti
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.

3386 posts
  • Australia
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Interviewed on the Envato Notes blog
  • Microlancer Beta Tester
  • Sold between 1 000 and 5 000 dollars
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

624 posts
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Bought between 10 and 49 items
  • Has been a member for 0-1 years
  • Haiti
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.

90 posts
  • Microlancer Beta Tester
  • Has been a member for 1-2 years
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Bought between 1 and 9 items
s4nji says

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

3386 posts
  • Australia
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Interviewed on the Envato Notes blog
  • Microlancer Beta Tester
  • Sold between 1 000 and 5 000 dollars
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