0

我尝试转换一些价格:

[0] => EUR 19,06 
[1] => 19, 70 € 
[2] => 42.53 €
[3] => 18€65 
[4] => 19,99 € 
[5] => 18€65
[6] => 23€95 
[7] =>      19,99 €  

转换成这种格式:xx.xx €</p>

我使用这个正则表达式:

/(EUR|)\s*(\d{1,})\s*(\.|,|&euro;|€|)\s*(\d{1,}|)\s*(&euro;|€|&nbsp;&euro;|&nbsp;€|)\s*/

并将这个面具变成preg_replace

$match = '${2}.$4 €';

它工作得很好,除了第 5 个条目:19,99 欧元。这有什么问题?

4

1 回答 1

0

我在您的正则表达式中看不到任何错误,但它可能更短:

/^(?=.*(?:EUR|€|euro))\D*(\d+)\D*(\d*)\D*$/

并作为掩码

$match = '$1.$2 €';

解释:

^                   # from start
(?=.*               # positive lookahead
    (?:EUR|€|euro)  # look for one of these
)                   # to take sure it is about € money
\D*(\d+)            # group at least + one digit in front of as many as possible non-digits
\D*(\d*)            # again to take the cents (* means zero or more)
\D*                 # take the remaining not digits
$                   # till the end

正则表达式住在这里。

希望能帮助到你。

于 2015-11-25T18:24:07.277 回答