php preg_replace 从末尾开始使用 $

php preg_replace using $ from the end

我正在尝试使用 php

从结束点或逗号和零开始

字符串是 €69,00

代码是return preg_replace('/[\.,]{1}0*$/', '', $_price);

使用美元从右开始搜索;找到多个零,直到找到第一个逗号或点(从右边开始)。将其替换为空字符串。

原始请求:显示格式化的整数价格,作为不带小数的底价。

字符串 1 进 €69,00€69

字符串 2 在 €69,80€69,80

之外

字符串 3 在 €69,95€69,95

之外

不知何故这不起作用

问题:当字符串已经格式化(不是数字)

时,如何修复正则表达式使整数值不带小数显示

试试这个

preg_replace('/([€$])(\d+)([.,])(\d+)/', '', $_price);

另一种变体是 寻找 ,00:

(€\d+)(?:[,.]00)

... 并将其替换为 </code>,请参阅 <a href="https://regex101.com/r/hIt0DZ/2/" rel="nofollow noreferrer"><strong>a demo on regex101.com</strong></a>。 <hr> 在 <code>PHP 中是:

<?php

$string = <<<DATA
String 1 in €69,00 and out €69
String 2 in €69,80 and out €69,80
String 3 in €69,95 and out €69,95
DATA;

$regex = '~(€\d+)(?:[,.]00)~';

$string = preg_replace($regex, "", $string);

echo $string;
?>