dongping4273 2017-08-27 12:59
浏览 16
已采纳

如何从Euro(€)表达式中删除特定的前导和尾随字符?

I have a string which contains a price inside.

I need to remove the decimal part and currency part of that.

The currency symbol can be removed by PHP using the str_replace() function, but the decimal part varies from product to product.

<span class="price" id="old-price-3">€&nbsp;200,00 </span>
    <span class="price" id="product-price-3">€&nbsp;80,00</span>

I need this like:

<span class="price" id="old-price-3">200 </span>
        <span class="price" id="product-price-3">80</span>

I tried str_replace():

echo str_replace(array(',00','€'),'','<span class="price" id="old-price-3">200 </span>
                <span class="price" id="product-price-3">80</span>');

But this only works when there are 00 decimals. Can someone help me with this?

  • 写回答

2条回答 默认 最新

  • doufu5747 2017-08-27 13:20
    关注

    You don't need more than one function call for this.

    Match the then zero or more non-digits, then capture one or more digits, then match anything that comes before the end span tag. Replace with the captured match.

    Code: (Demo) (Pattern Demo)

    $string='<span class="price" id="old-price-3">€&nbsp;200,00 </span>
    <span class="price" id="product-price-3">€&nbsp;80,00</span>';
    
    echo preg_replace("/€\D*(\d+)[^<]*/","$1",$string);
    

    Output:

    <span class="price" id="old-price-3">200</span>
    <span class="price" id="product-price-3">80</span>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据