从字符串中删除€符号


Remove € sign from string

我使用以下代码显示Magento商店内的价格:

<?php
    $myPrice = $_coreHelper->formatPrice($_price + $_weeeTaxAmount, false);
    $zeros = substr($myPrice, -2);
    if(strval($zeros) == "00") {
        $myPrice = substr($myPrice, 0, -2);
        $myPrice = $myPrice . '-';
    }
    echo '<span class="price">'.$myPrice.'</span>';
?>

但是我还想从这个字符串中删除€符号。

我怎样才能解决这个问题?

str_replace($search,$replace,$string)可能是您正在寻找的功能。

看一下这行代码:

$myPrice = str_replace("€","",$myPrice);

这将在字符串中搜索€并将其替换为空字符串,这意味着它将删除€。

参考PHP文档了解更多信息。也就是说,你也可以使用数组来搜索$search和$replace (PHP文档中的示例)

完整的示例:

<?php 
$myPrice = $_coreHelper->formatPrice($_price + $_weeeTaxAmount, false);
$zeros = substr($myPrice, -2);
if(strval($zeros) == "00") { $myPrice = substr($myPrice, 0, -2);
$myPrice = $myPrice . '-'; }
$myPrice = str_replace("€","",$myPrice);
//or if the € is htmlencoded
$myPrice = str_replace("&euro;","",$myPrice);
echo '<span class="price">'.$myPrice.'</span>'; 
?>

使用php str_replace:-

str_replace("€","",$yourstring);

此函数为binary-safe

str_replace(find,replace,string,count)