Validação do CPF em PHP

Função para validação do número do CPF em PHP.

Como todos sabem o cpf é validado através de um cálculo númerico e, como é mais facil fazer q explicar, segue abaixo a função para validação.

Obs.: Vale à pena frizar, para validação de um cpf não é necessário acessar uma base de dados da receita federal e sim efetuar um cálculo matemático para validar a seguencia numérica com o digito verificador. Vale lembrar ainda que a validação do mesmo não garante a total veracidade do número, pois podemos gerar números (se soubermos a regra de validação) que são válidos mas não são existentes.

<?php 

// Função que valida o CPF
function validar_cpf($cpf){
  
$cpf str_pad(ereg_replace('[^0-9]'''$cpf), 11'0'STR_PAD_LEFT);
  if(
strlen($cpf) != 11 || $cpf == '00000000000' || $cpf == '11111111111' || $cpf == '22222222222' || $cpf == '33333333333' || $cpf == '44444444444' || $cpf == '55555555555' || $cpf == '66666666666' || $cpf == '77777777777' || $cpf == '88888888888' || $cpf == '99999999999'){
   return 
False;
  }else{
      for(
$t 9$t 11$t++){
        for (
$d 0$c 0$c $t$c++) {
          
$d += $cpf{$c} * (($t 1) - $c);
        }
        
$d = ((10 $d) % 11) % 10;
        if (
$cpf{$c} != $d) {
          return 
False;
        }
      }
      return 
True;
   }
}

?>
Postado Por Thomas Jefferson em 12/08/2011 08:47:12
Visualizações 282860 | Comentários 10

Comentários

  • Nereida em 26/07/2020 15:52:36

    This survival grenade is a mini survival kit designed to help you survive in an emergency
    and it_s totally free no joke no market trick.. its free.
    Small portable and essential equipment to help you survive those vital first 48 hours of a civil war or unrest.
    Included
    _ Paracord (9ft)
    _ Carabiner
    _ Scalpel (2.25in)
    _ Alcohol Prep Pad
    _ Fishing Hooks (1 sml, 2 lrg)
    _ Fishing Floats (2)
    _ Fishing Weights (2)
    _ Fishing Line
    _ Jigs (2)
    _ Safety Pins (2)
    _ Needle
    _ Wire
    _ Aluminum Foil (1 sq ft)
    _ Ferro Rod Firestarter
    _ Tinder
    We live in unsettling times, are you prepared?
    Check it out for yourself s://.totalfreebie.com/totally-free-survival-grenade/
    Normal retail price $14.99
    All we are ask for is that you pay the standard postage, the actual product it free of charge.
    Shipping within USA is $6.95USD

  • Dacia em 28/07/2020 13:00:21

    This survival grenade is a mini survival kit designed to help you survive in an emergency
    and it_s totally free no joke no market trick.. its free.
    Small portable and essential equipment to help you survive those vital first 48 hours of a civil war or unrest.
    Included
    _ Paracord (9ft)
    _ Carabiner
    _ Scalpel (2.25in)
    _ Alcohol Prep Pad
    _ Fishing Hooks (1 sml, 2 lrg)
    _ Fishing Floats (2)
    _ Fishing Weights (2)
    _ Fishing Line
    _ Jigs (2)
    _ Safety Pins (2)
    _ Needle
    _ Wire
    _ Aluminum Foil (1 sq ft)
    _ Ferro Rod Firestarter
    _ Tinder
    We live in unsettling times, are you prepared?
    Check it out for yourself s://.totalfreebie.com/totally-free-survival-grenade/
    Normal retail price $14.99
    All we are ask for is that you pay the standard postage, the actual product it free of charge.
    Shipping within USA is $6.95USD

  • Ash em 19/04/2021 04:30:56

    Hello,

    My Name is Ash and I Run Tech Know Space s://techknowspace.com We are your Premium GO-TO Service Centre for All Logic Board & Mainboard Repair

    When other shops say "it can't be fixed" WE CAN HELP!

    ALL iPHONE 8 & NEWER
    BACK GLASS REPAIR - 1 HOUR

    Devices We Repair
    Audio Devices Audio Device Repair

    Bluetooth Speakers - Headphones - iPod Touch
    Computers All Computer Repair

    All Brands & Models - Custom Built - PC & Mac
    Game Consoles Game Console Repair

    PS4 - XBox One - Nintendo Switch
    Laptops All Laptop Repair

    All Brands & Models - Acer, Asus, Compaq, Dell, HP, Lenovo, Toshiba
    MacBooks All MacBook Repair

    All Series & Models - Air, Classic, Pro
    Phones All Phone Repair

    All Brands & Models - BlackBerry, Huawei, iPhone, LG, OnePlus, Samsung, Sony
    Smart Watches Apple Watch Repair

    Apple Watch - Samsung Gear - Moto 360
    Tablets All Tablet Repair

    All Brands & Models - iPad, Lenovo Yoga, Microsoft Surface, Samsung Tab

    Drone Repair

    Call us and tell us your issues today!

    Toll Free: (888) 938-8893
    s://techknowspace.com

    Ash Mansukhani
    [email protected]

  • Merziuziy em 29/06/2021 06:48:21

    rz7xMq s://xnxxx.web.fc2.com/ xnxx

  • johnanz em 22/07/2021 18:17:12

    write my essays writemyessayforme.web.fc2.com

  • Fifa55 em 11/08/2021 23:42:11

    Canada>Canada ://tubeadvisor.online rockettube The temperature is further lowered by the replacement of virtually all the dialogue with spoken passages of gnomic portentousness, drawn mostly the writings of Borges and haltingly delivered by the baffled singers.

  • Dwain em 11/08/2021 23:42:11

    Remove card ://porntuberating.online voyeurweb Well said Larson...I have been in and out of QEH for the past few years and I can honestly say I have received quality care. Can't please everyone but I would choose this hospital before any other. Nurses need to be praised for the hard job they do. The media need to stop reporting all the bad and focus on the good....like saving peoples lives day in day out as part of their 'job'.

  • Carol em 11/08/2021 23:42:11

    What company are you calling ? ://xnxxrating.online youporn &8220;The documents that we released are really the nation&8217;s memory,&8221; Mark Dunton, the National Archives&8217; principal contemporary records specialist, told ABC News. &8220;History has no final draft. There are new revelations &8212; and perhaps more still to come.&8221;

  • Lowell em 11/08/2021 23:42:12

    A packet of envelopes ://tubeadvisor.online nudevista "There hasn't actually been a debate on how it can besolved. As of next month, I think we should have that debate andhave it in depth and work on solutions," Dijsselbloem toldreporters on entering a talks of EU finance ministers.

  • Clinton em 11/08/2021 23:42:12

    Have you read any good books lately? ://porntuberating.online beeg Liz Cheney said on Tuesday that she intends to run for Senate in Wyoming, making her first foray into electoral politics by taking the rare step of challenging a sitting Republican incumbent, Sen. Mike Enzi.

Deixe seu comentário

Todos os comentários estão sujeitos à moderação