Source for file ptBR.php
Documentation is available at ptBR.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
* Specific validation methods for data used in Brazil
* This source file is subject to the New BSD license, That is bundled
* with this package in the file LICENSE, and is available through
* http://www.opensource.org/licenses/bsd-license.php
* If you did not receive a copy of the new BSDlicense and are unable
* to obtain it through the world-wide-web, please send a note to
* pajoye@php.net so we can mail you a copy immediately.
* @author Silvano Girardi Jr. <silvano@php.net>
* @author Marcelo Santos Araujo <msaraujo@php.net>
* @copyright 1997-2005 Silvano Girardi Jr.
* @license http://www.opensource.org/licenses/bsd-license.php new BSD
* @version CVS: $Id: ptBR.php,v 1.18 2008/11/27 00:33:19 ifeghali Exp $
* @link http://pear.php.net/package/Validate_ptBR
* Data validation class for Brazil
* This class provides methods to validate:
* - Postal code CEP (Código de Endereçamento Postal)
* - CPF (Cadastro de Pessoa Física)
* - CNPJ (Cadastro Nacional de Pessoa Jurídica)
* - Regions - brazilian states (Estados brasileiros)
* - Phone Numbers - brazilian phone numbers
* - Vehicle Plates - brazilian vehicle's plate
* @author Silvano Girardi Jr. <silvano@php.net>
* @author Marcelo Santos Araujo <msaraujo@php.net>
* @copyright 1997-2005 Silvano Girardi Jr.
* @license http://www.opensource.org/licenses/bsd-license.php new BSD
* @version Release: @package_version@
* @link http://pear.php.net/package/Validate_ptBR
* Validate CEP (Código de Endereçamento Postal, like postcode in US
* format: xxxxx-xxx,xxxxx xxx,xxxxxxxx
* @param string $postalCode pt_BR CEP/postalCode to validate
* @param bool $strong optional; strong checks (e.g. against a list
* of postcodes) (not implanted)
* @return bool true if $cep is ok, false otherwise
public static function postalCode($postalCode, $strong = false )
* Validade CPF (Cadastro de Pessoa Física)
* @param string $cpf CPF to validate
* @return bool true if $cpf is ok, false otherwise
public static function cpf($cpf)
if(!preg_match("/^\d{3}\.?\d{3}\.?\d{3}\.?-?\d{2}$/", $cpf)) {
} elseif (in_array($cpf, array ("00000000000", "11111111111",
"22222222222", "33333333333",
"44444444444", "55555555555",
"66666666666", "77777777777",
"88888888888", "99999999999"))) {
$sum = 10* $number[0 ]+9* $number[1 ]+8* $number[2 ]+7* $number[3 ]+
6* $number[4 ]+5* $number[5 ]+4* $number[6 ]+3* $number[7 ]+
$sum -= (11* (intval($sum/11 )));
if ($sum == 0 || $sum == 1 ) {
if ($result1 == $number[9 ]) {
$sum = $number[0 ]*11+ $number[1 ]*10+ $number[2 ]*9+ $number[3 ]*8+
$number[4 ]*7+ $number[5 ]*6+ $number[6 ]*5+ $number[7 ]*4+
$number[8 ]*3+ $number[9 ]*2;
$sum -= (11* (intval($sum/11 )));
if ($sum == 0 || $sum == 1 ) {
if ($result2 == $number[10 ]) {
* Validade CNPJ (Cadastro Nacional de Pessoa Jurídica)
* @param string $cnpj CNPJ to validate
* @return bool true if $cnpj is ok, false otherwise
public static function cnpj($cnpj)
if(!preg_match("/^\d{2}\.?\d{3}\.?\d{3}\/?\d{4}-?\d{2}$/", $cnpj))
} elseif ($cnpj == '00000000000000') {
$sum = $number[0 ]*5+ $number[1 ]*4+ $number[2 ]*3+ $number[3 ]*2+
$number[4 ]*9+ $number[5 ]*8+ $number[6 ]*7+ $number[7 ]*6+
$number[8 ]*5+ $number[9 ]*4+ $number[10 ]*3+ $number[11 ]*2;
$sum -= (11* (intval($sum/11 )));
if ($sum == 0 || $sum == 1 ) {
if ($result1 == $number[12 ]) {
$sum = $number[0 ]*6+ $number[1 ]*5+ $number[2 ]*4+ $number[3 ]*3+
$number[4 ]*2+ $number[5 ]*9+ $number[6 ]*8+ $number[7 ]*7+
$number[8 ]*6+ $number[9 ]*5+ $number[10 ]*4+ $number[11 ]*3+
$sum -= (11* (intval($sum/11 )));
if ($sum == 0 || $sum == 1 ) {
if ($result2 == $number[13 ]) {
* Validates a "region" (i.e. state) code
* @param string $region 2-letter state code
* @return bool true if $region is ok, false otherwise
public static function region($region)
* Validates a brazilian (ptBR) phone number.
* Also allows the formats
* If $requiredAreaCode is true:
* (XX)-XXXX-XXXX,(XX) XXXX XXXX, (XX)-XXXX XXXX, (XX) XXXX-XXXX,
* XX-XXXX-XXXX,XX XXXX XXXX,XX-XXXX XXXX,XX XXXX-XXXX,XX XXXXXXXX,(XX)XXXXXXXX
* If $requiredAreaCode is false: XXXX-XXXX,XXXX XXXX, XXXXXXXX
* @param string $number phone to validate
* @param bool $requireAreaCode require the area code? (default: true)
* @return bool The valid or invalid phone number
public static function phoneNumber($number, $requireAreaCode = true )
if (preg_match("/^(\d{4})[- ]?(\d{4})$/", $number)) {
$exp = "/^(\()?[1-9]{2}(?(1)\))[- ]?(\d{4})[- ]?(\d{4})$/";
* Validates a brazilian (ptBR) vehicle's plate
* Also allows the following formats
* XXX-XXXX,XXX XXXX,XXXXXXX
* The first three chars are [A-Z]
* The last four chars are [0-9]
* @param string $reg vehicle's plate
public static function carReg($reg)
* Validate PIS (Programa de Integracao Social)
* Allows the following formats
* XXX.XXX.XXX-XX, XXXXXXXXXXX or
* any combination with dots and/or hyphens
* @param string $pis pis code
public static function pis($pis)
$pis = str_replace (array ("-","."), "", $pis);
$multiplier = array (3 ,2 ,9 ,8 ,7 ,6 ,5 ,4 ,3 ,2 );
for ($i = 0; $i < 10; $i++ ) {
$sum += (int) $pis[$i] * $multiplier[$i];
$mod = ($mod < 2 ) ? 0 : 11 - $mod;
return ( (int) $pis[10 ] === $mod);
Documentation generated on Wed, 26 Nov 2008 20:00:06 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|