1

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?

4
  • More of a suggestion for the future than an answer in the present, but if you're checking multiple conditions like this, you may wish to return descriptive messages for each check, like "$binary is not numeric" for the is_numeric test, and then return something like "Correct" and the end of the function, if none of the other returns executed. This helps you identify which check is not behaving as you'd like. On a side note, I think your regex requirement of the string containing only 1s and 0s makes the is_numeric check unnecessary. Commented Jan 30, 2010 at 12:20
  • @Lazy Bob: I think he doesn't understand the concept of return very well, the strlen($binary) > 100 check makes me awfully suspicious he's trying to do a completely different thing. Commented Jan 30, 2010 at 12:24
  • 1
    A return value of "1" means error in the "original" implementation (see test case). Whether those tests make sense or not is hard or virtually impossible to decide for us (given the small amount of information we have). My only complain about it right now is that the function does more than the name implies (...and maybe the strange choice of return values =)) Commented Jan 30, 2010 at 12:33
  • @mememememe: Do you want to consider 01234567890123456789 ... {104 chars} for instance as a valid binary number? Commented Jan 30, 2010 at 12:58

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

preg_match() either returns 0 or 1 so the 0!== is unnecessary.
@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#)
I see, return (bool) preg_match(...); would be more explicit though.
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!
@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".
0
function checkbinary($bin) {
    return preg_match('#^[01]+$#', $bin);
}

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

2 Comments

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

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.