Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
金額を通貨で返す正規表現を持ってpreg_match('/(?<=\$)\d+(\.\d+)?\b/', $str, $regsいますが、金額に関連付けられたシンボルも取得しようとしています。
preg_match('/(?<=\$)\d+(\.\d+)?\b/', $str, $regs
1) たとえば、文字列は$300.00 asking price$300.00 を返すはずですが、今は 300 を返します 2) たとえば、文字列はEUR 300.00 EUR300.00 を返すはずですが、今は 300 を返します
$300.00 asking price
EUR 300.00
単純に、通貨と金額が必要です。
ありがとう
最初に、 または のいずれかの通貨を照合し$、EURその後にオプションの空白を続けます。
$
EUR
(?:EUR|[$])\s*
次に、主要な数字グループに一致し、その後にオプションのピリオドと 2 つの数字が続きます。
\d+(?:\.\d{2})?
合計すると、次のようになります。
$pattern = '/(?:EUR|[$])\s*\d+(?:\.\d{2})?/'; if (preg_match($pattern, $string, $matches)) { echo $matches[0]; }