Created
May 24, 2012 21:17
-
-
Save netojoaobatista/2784286 to your computer and use it in GitHub Desktop.
Validação usando expressão regular para aceitar apenas letras minúsculas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Verifica se uma string é UTF-8 | |
* @param string $string A string que será verificada | |
* @return boolean | |
*/ | |
function is_utf8($string) { | |
return (bool) preg_match('%^(?:'. | |
'[\x09\x0A\x0D\x20-\x7E]|'. | |
'[\xC2-\xDF][\x80-\xBF]|'. | |
'\xE0[\xA0-\xBF][\x80-\xBF]|'. | |
'[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|'. | |
'\xED[\x80-\x9F][\x80-\xBF]|'. | |
'\xF0[\x90-\xBF][\x80-\xBF]{2}|'. | |
'[\xF1-\xF3][\x80-\xBF]{3}|'. | |
'\xF4[\x80-\x8F][\x80-\xBF]{2}'. | |
')*$%xs', | |
$string); | |
} | |
/** | |
* Tem apenas letras minúsculas? | |
* @param string $string A string que será verificada | |
* @param boolean $allowNumbers Permite números na string? | |
* @param boolean $allowSpace Permite espaços na string? | |
*/ | |
function onlyHasLowerCase($string, $allowSpace = true, $allowNumbers = false) { | |
return (bool) preg_match('/^([a-z]|'. | |
($allowNumbers?'[0-9]|':null). | |
($allowSpace?'\s|':null). | |
'\xc3[\xa0-\xa5]|'. | |
'\xc3\xa7|'. | |
'\xc3[\xa8-\xab]|'. | |
'\xc3[\xac-\xaf]|'. | |
'\xc3([\xb2-\xb6]|\xb8)|'. | |
'\xc3[\xb9-\xbc])+$/', | |
is_utf8($string)?$string:utf8_encode($string)); | |
} | |
//Permite espaços em branco, mas não permite números | |
var_dump(onlyHasLowerCase('joão batista neto')); //bool(true) | |
//Não permite espaços em branco, nem números | |
var_dump(onlyHasLowerCase('joão batista neto',false)); //bool(false) | |
//Não permite espaços em branco, nem números | |
var_dump(onlyHasLowerCase('joãobatistaneto',false)); //bool(true) | |
//Permite espaços em branco, mas não permite números | |
var_dump(onlyHasLowerCase('joão batista neto 123')); //bool(false) | |
//Não permite espaços em branco, nem números | |
var_dump(onlyHasLowerCase('joãobatistaneto123',false)); //bool(false) | |
//Não permite espaços em branco, mas permite números | |
var_dump(onlyHasLowerCase('joãobatistaneto123',false,true)); //bool(true) | |
//Permite espaços em branco e números | |
var_dump(onlyHasLowerCase('joão batista neto 123',true,true)); //bool(true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment