6

如果我有这些字符串:

$string1 = "This book costs €25.99 in our shop."

而在另一边

$string2 = "This book costs 25,99€ in our shop."

如何使用preg_match获得“€25.99”或“25,99€” ?代码会是什么样子?

请注意,欧元符号有两种书写方式。欧盟的正确方法是在25,99€ 之类的数字后写符号,并使用逗号作为分隔符。然而,很多美国人都坚持使用美元方式(25.99 欧元)并将点作为最小分隔符

如何对这两种情况进行检查并以最干净和最有效的方式获取符号值?

4

3 回答 3

4
于 2011-08-04T19:09:49.520 回答
2
于 2011-08-04T20:47:07.120 回答
0

Check for both cases:

/([$€]?[\d,]+[$€]?)/

The ? makes the [$€] optional (literally '0 or 1 of...'), so you'd have to check for the degenerate case where there's just a bare number with no currency symbol at all.

于 2011-08-04T19:11:08.657 回答