正则表达式 - 剥离非数字并删除分(如果有)

发布于 2024-10-11 21:53:23 字数 232 浏览 8 评论 0原文

我目前正在开发一个 PHP 项目,需要一些正则表达式的帮助。我希望能够获取用户输入的货币值并删除所有非数字和小数位/分。

例如:

“2.000,00”到“2000”
“$2.000,00”到“2000”
“2abc000”到“2000”
“2.000”到 2000

(我使用的是非美国货币格式)

我该如何执行此操作?我很感激你的帮助 - 谢谢

I'm currently working on a project in PHP and I'm in need of some Regex help. I'd like to be able to take a user inputted monetary value and strip all non numeric and decimal places/cents.

Ex:

'2.000,00' to '2000'
'$ 2.000,00' to '2000'
'2abc000' to '2000'
'2.000' to 2000

(I'm using non US currency formatting)

How can I do this? I'd appreciate the help - Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌逼全场 2024-10-18 21:53:23

你可以这样做:

$str = preg_replace('/[^0-9,]|,[0-9]*$/','',$str); 

You can do:

$str = preg_replace('/[^0-9,]|,[0-9]*$/','',$str); 
ゞ记忆︶ㄣ 2024-10-18 21:53:23
$output = preg_replace('/[^0-9]/s', '', $input);

应该用空字符串替换非数字字符。

$output = preg_replace('/[^0-9]/s', '', $input);

that should replace non numeric chars with empty strings.

凉薄对峙 2024-10-18 21:53:23

这应该做你想做的。

$your_string_without_letters = preg_replace('\w+', '', $your_string) 
preg_match('[0-9][0-9.]*', $your_string_without_letters, $matches);
$clean_string = $matches[0];

一旦找到第一个数字,匹配就会开始,当它遇到既不是数字也不是点的东西时(即示例中的逗号或字符串结尾),匹配就会停止

编辑:忘记删除里面的字母价值第一。

(只是个人意见,但如果用户写入的字符不是数字、点、逗号或货币符号,我会拒绝输入,而不是尝试清理它)

This should do what you want.

$your_string_without_letters = preg_replace('\w+', '', $your_string) 
preg_match('[0-9][0-9.]*', $your_string_without_letters, $matches);
$clean_string = $matches[0];

The match will start as soon as the first number is found, and stop when it hits something that is neither a number nor a dot (ie. a comma or the end of the string in your examples)

EDIT : forgot to remove the letters inside the value first.

(Just a personal opinion, but if a user writes chracters that are not numbers, dots, commas or currency symbols I would refuse the input instead of trying to clean it)

满地尘埃落定 2024-10-18 21:53:23

在客户端,我在输入上使用类:

$("input.intgr").keyup(function (e) { // Filter non-digits from input value.
if (/\D/g.test($(this).val())) $(this).val($(this).val().replace(/\D/g, ''));
});
$("input.nmbr").keyup(function (e) { // Filter non-numeric from input value.
var tVal=$(this).val();
if (tVal!="" && isNaN(tVal)){
tVal=(tVal.substr(0,1).replace(/[^0-9\.\-]/, '')+tVal.substr(1).replace(/[^0-9\.]/, ''));
var raVal=tVal.split(".")
if(raVal.length>2)
tVal=raVal[0]+"."+raVal.slice(1).join("");
$(this).val(tVal);
}
});
$("input.money").keyup(function(){ money($(this)) })
.blur(function(){ money($(this),1); });
//----------- free-standing functions --------------
function money($inElem,inBlur,inDec){//enforces decimal - only digits and one decimal point. inBlur bool for final slicing to sets of 3 digits comma delimted
var isBlur=inBlur||0;//expects boolean (true/false/0/1 all work), default to 0 (false)
var dec=inDec || 2;
if(/[^,.0-9]/g.test($inElem.val()))//if illegal chars, remove and update
$inElem.val($inElem.val().replace(/[^,.0-9]/g, ""));
var ra=$inElem.val().split(".");
if(ra.length>2 || ra.length>1 && ra[ra.length-1].length>2){//if too more than 1 "." or last segment more than dec digit count, fix and update
if(ra[ra.length-1].length>2) ra[ra.length-1]=ra[ra.length-1].substr(0,dec);//shorten last element to only dec digit count
$inElem.val(ra.slice(0,ra.length-1).join("")+"."+ra[ra.length-1]);//glom all but last elem as single, concat dec pt and last elem
}
if(inBlur){
ra=$inElem.val().split(".");
var rvsStr=zReverse(ra[0].replace(/,/g,""));
var comDelim="";
while(rvsStr.length>0){
comDelim+=rvsStr.substr(0,3)+",";
rvsStr=rvsStr.substr(3);
}
$inElem.val(zReverse(comDelim).substr(1)+(ra.length==2?"."+ra[1]:""));
}
}
function zReverse(inV){//only simple ASCII - breaks "foo

On the client side I use classes on the inputs:

$("input.intgr").keyup(function (e) { // Filter non-digits from input value.
    if (/\D/g.test($(this).val())) $(this).val($(this).val().replace(/\D/g, ''));
});
$("input.nmbr").keyup(function (e) { // Filter non-numeric from input value.
    var tVal=$(this).val();
    if (tVal!="" && isNaN(tVal)){
        tVal=(tVal.substr(0,1).replace(/[^0-9\.\-]/, '')+tVal.substr(1).replace(/[^0-9\.]/, ''));
        var raVal=tVal.split(".")
        if(raVal.length>2)
            tVal=raVal[0]+"."+raVal.slice(1).join("");
        $(this).val(tVal);
    } 
});
$("input.money").keyup(function(){ money($(this)) })
    .blur(function(){ money($(this),1); });
//----------- free-standing functions --------------
function money($inElem,inBlur,inDec){//enforces decimal - only digits and one decimal point. inBlur bool for final slicing to sets of 3 digits comma delimted
    var isBlur=inBlur||0;//expects boolean (true/false/0/1 all work), default to 0 (false)
    var dec=inDec || 2;
    if(/[^,.0-9]/g.test($inElem.val()))//if illegal chars, remove and update
        $inElem.val($inElem.val().replace(/[^,.0-9]/g, ""));
    var ra=$inElem.val().split(".");
    if(ra.length>2 || ra.length>1 && ra[ra.length-1].length>2){//if too more than 1 "." or last segment more than dec digit count, fix and update
        if(ra[ra.length-1].length>2) ra[ra.length-1]=ra[ra.length-1].substr(0,dec);//shorten last element to only dec digit count
        $inElem.val(ra.slice(0,ra.length-1).join("")+"."+ra[ra.length-1]);//glom all but last elem as single, concat dec pt and last elem
    }
    if(inBlur){
        ra=$inElem.val().split(".");
        var rvsStr=zReverse(ra[0].replace(/,/g,""));
        var comDelim="";
        while(rvsStr.length>0){
            comDelim+=rvsStr.substr(0,3)+",";
            rvsStr=rvsStr.substr(3);
        }
        $inElem.val(zReverse(comDelim).substr(1)+(ra.length==2?"."+ra[1]:""));
    }
}
function zReverse(inV){//only simple ASCII - breaks "foo ???? bar mañana"
    return inV.split("").reverse().join("");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文