In PHP the bindec()
function can be used to convert a binary number to decimal, but there is no error checking to test if the binary number string is valid. What happens is that something like bindec('1051');
returns 5 instead of an error.
Is there an efficient way to validate if the number is a binary string?
if (preg_match('~^[01]+$~', $num)) {
// valid binary number
}
This should do:
if(preg_match("/^[0-1]+$/", $var)) {
// do something
}