简体   繁体   English

删除货币符号 PHP

[英]Remove Currency Symbol PHP

I have a form with a jQuery slider allowing a price range to be selected.我有一个带有 jQ​​uery 滑块的表单,允许选择价格范围。

The JavaScript that runs the slider also adds a prefix of the £ symbol to each price.运行滑块的 JavaScript 还会为每个价格添加一个£符号前缀。

I am trying to remove the £ symbol before the form is submitted, as the price values in the database are only numeric without any currency symbol.我试图在提交表单之前删除£符号,因为数据库中的价格值只是没有任何货币符号的numeric

I am trying the str_replace function but the code below is not working?我正在尝试str_replace函数,但下面的代码不起作用?

<div id="slider">
    <input type="text" id="price-min" name="price-min" <?php 
        $price-min = str_replace('£', '', $price-min);
    ?>>
    </input>
    <input type="text" id="price-max" name="price-max" <?php 
        $price-max = str_replace('£', '', $price-max);
    ?>>
    </input>
</div>

You can just filter the variable.您可以过滤变量。

<?php
$value = "£200";
echo filter_var($value, FILTER_SANITIZE_NUMBER_INT);

Live Preview实时预览

Edit编辑

I'll need to cover all values from £0 - £1,000 and also strip out the comma from the £1,000 figure - Is this possible?我需要涵盖从 0 英镑到 1,000 英镑的所有值,并从 1,000 英镑的数字中去掉逗号 - 这可能吗?

<?php
$value = "£200";
echo filter_var( str_replace(",", "", $value), FILTER_SANITIZE_NUMBER_INT);

Live Preview实时预览

Or even sanitize as a float甚至可以像漂浮物一样消毒

<?php
$value = "£1,200";
echo filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT);

Live Preview实时预览

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores有效的变量名称以字母或下划线开头,后跟任意数量的字母、数字或下划线

Your variables have - in them, making them invalid.您的变量中有-在其中,使它们无效。

Edit 2编辑 2

Removing the £ with javascript, use a regular expression.使用 javascript 删除£ ,使用正则表达式。

var s = "£1,300";
s.replace(/\D/g,'');

Live Preview实时预览

Edit 3编辑 3

If you're accepting negative decimal numbers (ie: -£20,000.06/-£20000.06), then use the FILTER_FLAG_ALLOW_FRACTION flag.如果您接受负十进制数(即:-£20,000.06/-£20000.06),则使用FILTER_FLAG_ALLOW_FRACTION标志。 This issue was pointed out by Milan as the current answers didn't handle this test-case.米兰指出了这个问题,因为当前的答案没有处理这个测试用例。

<?php
$value = "-£20,000.06";
echo filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);

Live Preview实时预览

Actually you can remove the pound sign after the form is submitted because php is a server side language.实际上,您可以在提交表单后删除井号,因为 php 是一种服务器端语言。 So either you remove it from the javascript slider so it won't be added to the value attribute or you use the function from hd.'s answer in the php script that the form points to.因此,要么将其从 javascript 滑块中删除,这样它就不会被添加到 value 属性中,要么您在表单指向的 php 脚本中使用 hd.'s answer 中的函数。

To remove all characters, but numbers, dots and minus signs you should use:要删除除数字、点和减号之外的所有字符,您应该使用:

$number = preg_replace('/[^0-9-.]+/', '', $string);

Once applied this function will return a numeric value, which can be correctly converted to float value.一旦应用此函数将返回一个数值,该数值可以正确转换为浮点值。 It will also keep the negative numbers intact, which is quite important.它还将保持负数不变,这非常重要。 The other solutions presented here, convert them to positive numbers, which is incorrect and dangerous, if dealing with monetary values.此处介绍的其他解决方案将它们转换为正数,如果处理货币价值,这是不正确且危险的。

Currency symbols in HTML are HTML entities(I mean, frontend value) so using signs of currency directly to find and replace them is not possible, so using as below to get only digit in PHP. HTML 中的货币符号是 HTML 实体(我的意思是前端值),因此无法直接使用货币符号来查找和替换它们,因此使用如下方式在 PHP 中仅获取数字。

$price = '$109.99';
$digitOnly = preg_replace('/&.*?;/', '', $price);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM