I want to subtract two numbers but both of them have currency. (25$-10$)
<?php
$tamount= $agreementdata['amount'];
$eamount= $agreementdata['earnestamount'];
$ramount= $agreementdata['amount'-'earnestamount'];
echo $ramount;
?>
CodePudding user response:
$currencyFrom = '25$';
$currency = '20$';
$results = (int)substr($currencyFrom, 0, -1) - (int)substr($currency, 0, -1);
echo $results;
I hope this will answer your question. Please mark it accepted if you are agree with the solution and idea.
CodePudding user response:
From
Converting to integer : From strings
If the string is numeric or leading numeric then it will resolve to the corresponding integer value, otherwise it is converted to zero (0).
So (int)'25$'
will return 25
, and add the dollar sign back on so that it can be understood by the array.
echo (int)'25$'-(int)'20$'.'$';
CodePudding user response:
<?php
$tamount= $agreementdata['amount'];
$eamount= $agreementdata['earnestamount'];
$ramount= $tamount - $eamount;
echo $ramount;
?>
CodePudding user response:
If you want more flexibility, I would work with regex.
$str = '25$ 10Rupiah 25 $ 10 Rupiah';
$pattern = '/[A-Z|\$]*/i';
echo preg_replace($pattern, '', $str);
// output 25 10 25 10
That will look for your case:
<?php
function getAmount($str) {
$pattern = '/[A-Z|\$]*/i';
return (int) preg_replace($pattern, '', $str);
}
$tamount= getAmount( $agreementdata['amount'] );
$eamount= getAmount( $agreementdata['earnestamount'] );
$ramount= $agreementdata['amount'-'earnestamount'];
echo $ramount;
?>