Skip to content

Instantly share code, notes, and snippets.

@lnoering
Created August 6, 2015 18:08
Show Gist options
  • Save lnoering/20a159c28442beeb936c to your computer and use it in GitHub Desktop.
Save lnoering/20a159c28442beeb936c to your computer and use it in GitHub Desktop.
Verificar o Estado pelo Cep.
public function getEstado($cep)
{
$estados = array('ac'=>'69900,69999',
'al'=>'57000,57999',
'am'=>'69000,69299|69400,69899',
'ap'=>'68900,68999',
'ba'=>'40000,48999',
'ce'=>'60000,63999',
'df'=>'70000,72799|73000,73699',
'es'=>'29000,29999',
'go'=>'72800,72999|73700,76799',
'ma'=>'65000,65999',
'mg'=>'30000,39999',
'ms'=>'79000,79999',
'mt'=>'78000,78899',
'pa'=>'66000,68899',
'pb'=>'58000,58999',
'pe'=>'50000,56999',
'pi'=>'64000,64999',
'pr'=>'80000,87999',
'rj'=>'20000,28999',
'rn'=>'59000,59999',
'ro'=>'76800,76999',
'rr'=>'69300,69399',
'rs'=>'90000,99999',
'sc'=>'88000,89999',
'se'=>'49000,49999',
'sp'=>'01000,19999',
'to'=>'77000,77999'
);
$comparar = str_split($cep, 5);
$comparar = $comparar[0];
foreach ($estados as $key => $value)
{
foreach (explode('|',$value) as $valores_cep)
{
$ceps = explode(',',$valores_cep);
if($comparar >= $ceps[0] && $comparar <= $ceps[1])
{
return $key;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment