开发者

Can you help me understand this simple PHP function, please?

I would like to know how this function works so I can re-write it in ColdFusion. I've never programmed in PHP. I think, the function checks if $string is 11 numbers in length.

  1. But how is it doing it?
  2. Why is it using a loop instead of a simple len() function?

I read about str_pad(), and understan开发者_开发问答d it. But what is the function of the loop iteration $i as a third argument? What's it doing?

if($this->check_fake($cpf, 11)) return FALSE;

function check_fake($string, $length)
{
   for($i = 0; $i <= 9; $i++) {
   $fake = str_pad("", $length, $i);
   if($string === $fake) return(1);
   }
}

The function is meant to validate a CPF, the Brazilian equivalent of a US SSN #. The CPF is 11 characters in length. Basically, I need to know what it's doing so I can write the function in Coldfusion.

If it's just a length, can't it be if (len(cpf) != 11) return false; ?

Here is the entire code snippet if it interests you:

    <?
    /*
    *@ Class VALIDATE - It validates Brazilian CPF/CNPJ numbers 
    *@ Developer: André Luis Cupini - andre@neobiz.com.br
    ************************************************************/
    class VALIDATE
    {
        /*
        *@ Remove ".", "-", "/" of the string
        *****************************************************/
        function cleaner($string)
        {
            return $string = str_replace("/", "", str_replace("-", "", str_replace(".", "", $string)));
        }

        /*
        *@ Check if the number is fake
        *****************************************************/
        function check_fake($string, $length)
        {
            for($i = 0; $i <= 9; $i++) {
                $fake = str_pad("", $length, $i);
                if($string === $fake) return(1);
            }
        }

        /*
        *@ Validates CPF
        *****************************************************/
        function cpf($cpf) 
        {
            $cpf = $this->cleaner($cpf);
            $cpf = trim($cpf);
            if(empty($cpf) || strlen($cpf) != 11) return FALSE;
            else {
                if($this->check_fake($cpf, 11)) return FALSE;
                else {
                    $sub_cpf = substr($cpf, 0, 9);
                    for($i =0; $i <=9; $i++) {
                        $dv += ($sub_cpf[$i] * (10-$i));
                    }
                    if ($dv == 0) return FALSE; 
                    $dv = 11 - ($dv % 11); 
                    if($dv > 9) $dv = 0;
                    if($cpf[9] != $dv) return FALSE;

                    $dv *= 2;
                    for($i = 0; $i <=9; $i++) {
                        $dv += ($sub_cpf[$i] * (11-$i));
                    }
                    $dv = 11 - ($dv % 11); 
                    if($dv > 9) $dv = 0;
                    if($cpf[10] != $dv) return FALSE;
                    return TRUE;
                }
            }
        }
}
?>


it basically makes sure the number isn't:

11111111111
22222222222
33333333333
44444444444
55555555555

etc...


With comments:

function check_fake($string, $length)
{
    // for all digits
    for($i = 0; $i <= 9; $i++) 
    {
        // create a $length characters long string, consisting of
        // $length number of the number $i
        // e.g. 00000000000
        $fake = str_pad("", $length, $i);
        // return true if the provided CPN is equal
        if($string === $fake) return(1);
    }
}

In short, it tests to see if the provided string is just the same digit repeated $length times.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜