Skip to content

Instantly share code, notes, and snippets.

@u01jmg3
Last active August 29, 2015 14:13
Show Gist options
  • Save u01jmg3/b7d6da6ce1eb7a2d5d77 to your computer and use it in GitHub Desktop.
Save u01jmg3/b7d6da6ce1eb7a2d5d77 to your computer and use it in GitHub Desktop.
Function to validate letters (incl. special chars) and numbers and to check the min/max size
<?php
/*
Allowed characters:
ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ
\x{00C0}-\x{00CF}\x{00D0}-\x{00D6}\x{00D8}-\x{00DF}\x{00E0}-\x{00EF}\x{00F0}-\x{00F6}\x{00F8}-\x{00FF}
[space] \n - , . / ! % & ' ( ) : ; ? @ _ £
\ \n\-\x{002C}\x{002E}\x{002F}\x{0021}\x{0025}-\x{0029}\x{003A}\x{003B}\x{003F}\x{0040}\x{005F}\x{00A3}
*/
function validate_letters_and_numbers_only($input, $max_size = '', $min_size = ''){
if(preg_match('/^[0-9a-zA-Z\x{00C0}-\x{00CF}\x{00D0}-\x{00D6}\x{00D8}-\x{00DF}\x{00E0}-\x{00EF}\x{00F0}-\x{00F6}\x{00F8}-\x{00FF}\ \n\-\x{002C}\x{002E}\x{002F}\x{0021}\x{0025}-\x{0029}\x{003A}\x{003B}\x{003F}\x{0040}\x{005F}\x{00A3}]+$/u', $input)){
//Some special chars take up more space in a database than a single normal char such as 'x'.
//ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ£
$input = preg_replace('/[\x{00C0}-\x{00CF}\x{00D0}-\x{00D6}\x{00D8}-\x{00DF}\x{00E0}-\x{00EF}\x{00F0}-\x{00F6}\x{00F8}-\x{00FF}\x{00A3}]/u', 'xx', $input);
if(is_numeric($min_size)){
if(strlen($input) < $min_size)
return false;
}
if(is_numeric($max_size)){
if(strlen($input) > $max_size)
return false;
}
return true;
}
return false;
}
//echo (validate_letters_and_numbers_only('Hello World!', 12)) ? 'Valid' : 'Invalid';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment