You cannot use splitting, but matching patterns to determine the amount and currency used. Because in some locales the currency symbol appears before the sum, in others - by the sum. In addition, in some locales, the symbol and number are separated by spaces.
You can use the following function:
function findAmountAndCurrency($s, &$amount, &$currency){ $re_amount="/[0-9\.]+/"; $re_curr="/[£\$€]+/"; preg_match($re_amount, $s, $matches); $amount = floatval($matches[0]); preg_match($re_curr, $s, $matches); $currency = $matches[0]; }
Here's how it will be used:
function handle($s){ $currencySymbols = array('£'=>'GBP', '$'=>'USD','€'=>'EUR'); findAmountAndCurrency($s, $amount, $currency); echo("Amount: " . $amount . "<br/>"); echo("Currency: " . $currency . "<br/>"); echo("Identified Currency: " . $currencySymbols[$currency] . "<br/>"); } handle("£12.10"); handle("3.212 €"); handle("$ 99.99");
You may have a problem with the EURO sign if you have a UTF-8 input. It is not possible to verify the solution right now. Maybe someone else can help.
source share