My regex doesn’t work as expected when I run my code, but when I test it on a website the regex does exactly as expected. See my function:
function checkDecimais($numero){
if($numero > 0){
preg_match('/^([0]+).([0]*)([1-9])/', $numero, $matches);
if(isset($matches[2])){
return strlen($matches[2]);
}else{
return 0;
}
}else{
return 0;
}
}
If I call it like this: checkDecimais(0.0009)
it works and returns the number 3, as it has 3 decimal zero digits. However, if I call it like this: checkDecimais(0.00009)
, it stops working and returns 0, because my regex is only taking up to 3 zero digits and ignoring when there are more than three digits?
If I change it to string checkDecimais('0.0009')
, it works, however I’m giving it numbers and I can’t understand why it doesn’t work.
Can anybody help me ?
I’ve already tried using other regex, but they all have the same problem, it only takes the first 3 zeros when the data is of type number.
Marcos Nobrega is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.