1

Is there a simple way to extract the money amount from a string using php?

For example, I have $1,419.99

I want to get in return 1419.99

Another example, I have $1,321

I want to get 1321 back.

I know I can manually remove $ and , but is there a way using something like preg_match to grab all the numbers and if any thing after the decimal, grab that as well and just return the number with no , or dollar sign? Seems like a simple solution but for some reason my mind is not wrapping around this.

3 Answers 3

2

Checkout NumberFormatter::parseCurrency

Here's the link to PHP documentation for it

Sign up to request clarification or add additional context in comments.

1 Comment

@John If PHP's parseCurrency can handle your use cases, this would be the way to go, as building a thorough regex would be substantially more work.
0

yes you can used explode function. Like

$amount = '$1,419.99';
$money = explode('$', $amount);
echo $money[1];

Results: 1,419.99

2 Comments

The OP wants the result to be 1419.99 (no thousand separator).
than also use echo str_replace(',','' ,$money[1]);
0

Hi If you want to use regular expression, you can try the below code

$amount = '$1,419.99';
$amount = preg_replace('/[^.a-zA-Z0-9]/s', '', $amount );
echo $amount;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.