Strip everything from a string apart from a number and currency denominator

白昼怎懂夜的黑 提交于 2019-12-12 16:23:53

问题


I have the following example strings:

The price is $54.00 including delivery
On sale for £12.99 until December
European pricing €54.76 excluding UK

From each of them I want to return only the price and currency denominator

$54.00
£12.99
€54.76

My though process is the have an array of currency symbols and search the string for each one and then capture just the characters before the space after that - however, $ 67.00 would then fail

So, can i run through an array of preset currency symbols, then explode the string and chop it at the next instance of a non numeric character that is not a . or , - or maybe with regex

Is this possible?


回答1:


In regex, \p{Currency_Symbol} or \p{Sc} represent any currency symbol.

However, PHP supports only the shorthand form \p{Sc} and /u modifier is required.


Using regex pattern

/\p{Sc}\s*\d[.,\d]*(?<=\d)/u

you will be able to match for example:

  • $1,234
  • £12.3
  • € 5,345.01

If you want to use . as a decimal separator and , as a thousands delimiter, then go with

/\p{Sc}\s*\d{1,3}(?:,\d{3})*(?:\.\d+)?/u

Check this demo.




回答2:


You could go for something like this:

preg_match('/(?:\$|€|£)\s*[\d,.-]+/', $input, $match);

And then find your currency and price inside $match.

Of course, you can generate that first part from an array of currency symbols. Just don't forget to escape everything:

$escapedCurrency = array_map("preg_quote", $currencyArray);
$pattern = '/(?:' . implode("|", $escapedCurrency) . ')\s*[\d,.-]+/';
preg_match($pattern, $input, $match);

Some possible improvement to the end of the pattern (the actual number):

(?:\$|€|£)\s*\d+(?:[.,](?:-|\d+))?

That will make sure that there is only one . or , followed by either - or only digits (in case your intention was to allow an international decimal separator).

If you only want to allow the comma to separate thousands, you could go for this:

(?:\$|€|£)\s*\d{1,3}(?:,\d{3})*(?:\.(?:-|\d+))?

This will match the longest "correctly" formatted number (i.e. $ 1,234.4567,123.456 -> $ 1,234.4567 or € 123,456789.12 -> € 123,456). It really depends on how accurate you want to go for.



来源:https://stackoverflow.com/questions/13441125/strip-everything-from-a-string-apart-from-a-number-and-currency-denominator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!