Skip to content

Instantly share code, notes, and snippets.

@reggiegutter
Created May 4, 2013 05:53

Revisions

  1. reggiegutter created this gist May 4, 2013.
    36 changes: 36 additions & 0 deletions regex_celular.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <?php
    // Função para checar número de telefone

    function phone_check($phone)
    {
    $exp_regular = '/^(\(11\) (9\d{4})-\d{4})|((\(1[2-9]{1}\)|\([2-9]{1}\d{1}\)) [5-9]\d{3}-\d{4})$/';
    $ret = preg_match($exp_regular, $phone);

    if($ret === 1)
    {
    echo 'Número de telefone válido';
    return TRUE;
    }
    else
    {
    echo 'Número de telefone inválido';
    return FALSE;
    }
    }

    // Essa expressão é feita mais para telefones celulares.
    // Você pode adicionar suporte para todos os números de telefone, ou criar outro regex com base nesse.
    // Se alguém encontrar uma forma de melhorá-lo ou se alguma coisa estiver errada, favor informa!

    // testes
    preg_match($exp_regular, '(11) 92222-2222'); // OK
    preg_match($exp_regular, '(12) 8222-2222'); // OK

    preg_match($exp_regular, '(11) 82222-2222'); // NOT OK
    preg_match($exp_regular, '(12) 92222-2222'); // NOT OK
    preg_match($exp_regular, '(11) 3222-2222'); // NOT OK

    // Espero ter ajudado!

    // Abraço a todos!
    ?>