2016-04-23 98 views
0

我有价格价值,其中前面有英镑符号。我需要删除该标志。PHP删除英镑符号(£)从价格

$price = '£3.76' (This comes from database and is stored as a string within the database) 

1) preg_replace('/[\£]/', '', $price); 
2) ltrim($price, '£'); 
3) str_replace(utf8_decode("£"),"",utf8_decode($price)); 
4) str_replace('£', '', $price); 

我试图使用一切,但没有任何工作。 1和4用Â替代,2和3在值的左边添加Â。有人可以帮帮我吗。

+0

以上哪个命令会产生您所看到的输出?请将包含价格值的完整字符串作为示例发布,以便我们有一个真实世界的示例来帮助您。 – nvisser

+0

我现在编辑了这个问题,请让我知道现在是否更清楚。 – user4676307

+2

如果您在浏览器中看到'',这是因为服务器发送的内容类型字符编码不正确。你可以添加'header('Content-type:text/html; charset = utf-8');'给你的脚本正确地看到井号。但'$ price = str_replace('£','',$ price);'对我来说可以很好地移除它。 – drew010

回答

0

我有过类似的情况。请尝试以下代码:

$amount = "$500"; 

echo substr($amount,1,strlen($amount)); // returns "500" 
+0

由于井号不是ASCII码,因此它使用2个字节,因此需要使用mb_substr。 – drew010

+0

@ drew010我现在用mb_substr替换第一个字符。但我仍然不认为这是正确的解决方案,因为镑符号可能不在字符串中 – user4676307