tags:

views:

223

answers:

4

I'm using this function to check if binary is correct, I know it looks sloppy.. I'm not sure how to write the function that well.. but it doesn't seem to work!

If binary = 10001000 it says malformed, even though it's not.. what is wrong in my function?..

function checkbinary($bin) {
    $binary = $bin;
    if(!strlen($binary) % 8 == 0){
        return 1;
    }
    if (strlen($binary) > 100) { 
        return 1;
    } 
    if (!preg_match('#^[01]+$#', $binary)){ //Tried without !
        return 1;
    } 
    if (!is_numeric($binary)) {
        return 1;   
    }
}

if (checkbinary("10001000") != 1) {
  echo "Correct";
} else {
  echo "Binary incorrect";
}

Why does this function always say 10001000 is incorrect?

A: 

How about using PHP's built-in is_binary function. It must be better implementation ofcourse.

Sarfraz
http://php.net/manual/en/function.is-binary.php says: `is_binary (PHP 6 >= 6.0.0)`
VolkerK
@Sarfraz Ahmed: `is_binary()` is not meant to check base 2 numbers.
Alix Axel
+4  A: 

if(!strlen($binary) % 8 == 0){ should be

if( strlen($binary) % 8 !== 0 ){

edit and btw: Since you're already using preg_match() you can simplify/shorten the function to

function checkbinary($binary) {
  return 1===preg_match('#^(?:[01]{8}){0,12}$#', $binary);
}

This allows 0 - 12 groups of 8 0/1 characters which includes all the tests you currently have in your function:

  • strlen()%8 is covered by {8} in the inner group
  • strlen() > 100 is covered by {0,12} since any string longer than 8*12=96 characters would trigger either the first if or the >100 test
  • 0/1 test is obvious
  • is_numeric is kinda superfluous

edit2: The name checkbinary might not be a perfect choice for the function. I wouldn't necessarily expect it to check for 8bit/byte alignment and strlen()<=100.

VolkerK
`preg_match()` either returns `0` or `1` so the `0!==` is unnecessary.
Alix Axel
@Alix: I wanted it to return true/false and since I don't like the type juggling for boolean types I've added an explicit test. (That's one thing I like about C#)
VolkerK
I see, `return (bool) preg_match(...);` would be more explicit though.
Alix Axel
A thousand thanks, It brought a smile to my face when I saw just one line, You know I should really learn some regex.I can use your explaination to learn from it, you're perfect!
oni-kun
@Alix: Maybe so, matter of choice. I change it to 1===preg\_match() since it could also return FALSE in case of an error and 1 is the only return value that means "match", anything else is a "failed".
VolkerK
A: 
function checkbinary($bin) {
    return preg_match('#^[01]+$#', $bin);
}

Some tests: http://www.ideone.com/3D9SQetX and http://www.ideone.com/1HCCtxVV.

Alix Axel
This is only a partial replacement for the "original" function as posted by mememememe.
VolkerK
@VolkerK: Check my comment on his question, it doesn't make a lot of sense.
Alix Axel
A: 

I'm not entirely sure what you're attempting to achieve, but you might be able to get there via the following...

if($binaryString == base_convert($binaryString, 2, 2))...

In essence, this is comparing the results of a conversion from binary to binary - hence if the resultant output is identical, the input must be a valid binary string.

middaparka
Throws warnings if a non-binary number is provided I believe.
Alix Axel