开发者

Regex - Strip non numeric and remove cents if any

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


You can do:

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


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

that should replace non numeric chars with empty strings.


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)


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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜