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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题