Reputation: 490
I have a string with integer and float value. Now I want to extract the value by matching couple of words.
My string looks like the following text. Every string has balance is text. Now I want to get the balance amount by PHP regular expression.
$str_demo_one = "210:Recharge request of TK 200 for mobile no. 1789000000, transaction ID BD07232231230125 is successful. Your account balance is TK 13769.98.";
$str_demo_two = "563:Recharge 150 Tk to 1880000000 is successful. Transaction number is R190723.2158.6600eb.Your new balance is tk 3852.55 TAKA. Thank You.";
$str_demo_three = "Recharge Request of TK 300 for mobile no 1942300468, transaction ID R190723.2152.2201e1 is successful. your account balance is TK 5434.72.";
$str_demo_four = "Recharge 650 Tk to 16300000 is successful. Transaction number is R190723.1106.710166.Your new balance is 5921.18 TAKA. Thank You.";
$str_demo_five = "Transaction ID is 0718140110124179. 1531141036 has been recharged successfully with 89 Taka. Your new balance is 2529 Taka.";
I've tried with flowing code
preg_match_all('/(balance is)(\D*)(\d+)/i',$str,$matches);
it only returns integer value not returning float value.
Upvotes: 1
Views: 127
Reputation: 163577
In your pattern you are not taking the decimal part into account. You might update your pattern to (balance is)(\D*)(\d+(?:\.\d+)?)
If you only want the match, you could make use of \K for forget what was matched, omit the capturing groups and make the decimal part optional.
\bbalance is \D*\K\d+(?:\.\d+)?
Upvotes: 1