0

I have following grouped currency formatted string:

$str = "$1,000,000";

I want to just extract 1000000 in order to do some calculation. How can I achieve that? Is there any built-in PHP function to do so? Otherwise I may need to use some RegExp.

EDIT

This is not a duplicate question because here we have a currency grouped formatted string and not a straightforward simple text.

Sachin
  • 1,646
  • 3
  • 22
  • 59

2 Answers2

4

You might replace all non-digits with the empty string:

$str = "$1,000,000";
print(
  preg_replace('/\D+/', '', $str)
);

Of course, "non-digits" includes decimal points too. If you want to keep decimal points, then use a negated character set instead:

$str = "$1,000,000.00";
print(
  preg_replace('/[^\d.]+/', '', $str)
);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • In `\D+`, is `+` necessary? I'm getting the same result even after removing it. – Sachin Sep 06 '18 at 03:25
  • 1
    No, it's not necessary, but it might be a bit more efficient. For example, with `$$$$5`, rather than replacing a single `$` 4 times, it'll just replace `$$$$` once. – CertainPerformance Sep 06 '18 at 03:26
  • I think you are not right because if i had `$$$1,000,000` then result still would be `1000000` without using `+` in `\D` – Sachin Sep 06 '18 at 03:29
  • 1
    The *result* will be the same, but there will be *fewer operations on the string* performed to get to that result. Like I said, it's not necessary, just more efficient. – CertainPerformance Sep 06 '18 at 03:29
  • @user5307298 `+` is one or more, without the quantifier you replace each character individually. Quantified is more efficient (although minimal in your example) see https://regex101.com/r/WbZnMk/2/ vs https://regex101.com/r/WbZnMk/1/ – user3783243 Sep 06 '18 at 03:47
  • @user3783243 I know `+` is a quantifier but as I saw in your both examples, using `+` or not made no difference at all. – Sachin Sep 06 '18 at 03:51
  • @user5307298 Look at the number of steps (top right) it takes. 18 vs. 14. – user3783243 Sep 06 '18 at 03:52
  • Be careful! This regexp will give you the wrong result if your input string contains a decimal value, for example `$str = "$1,000,000.00"`. The decimal point will also be stripped away and you'll end up with the number `100000000`. Perhaps you can guarantee that you'll never see numbers with decimal values for your case, but this is not a general purpose method to convert formatted currency strings to their numerical equivalents. – Sam Choukri Sep 06 '18 at 03:56
  • @user3783243 that i saw but practically visually does it make any difference since the result is same whether we use `+` or not. who one else is watching that `top right` difference on a website! – Sachin Sep 06 '18 at 04:01
  • @SamChoukri yes i know but we really don't need any decimal place here - just the integer value that's all. – Sachin Sep 06 '18 at 04:02
  • @user5307298 You said `without any solid example its hard to agree` I've given you the requested example. In your sample probably no difference would ever be noticed by a human, in larger examples it could be a huge difference. – user3783243 Sep 06 '18 at 04:04
1

Here is a str_replace way, assuming that your digits only contain $ and , (or . if you include decimals)

$toBeRemoved = array('$', ',');
$str = '$1,000,000';
$cleaned = floatval(str_replace($toBeRemoved, "", $str));
echo $cleaned;
Carl Binalla
  • 5,393
  • 5
  • 27
  • 46