I need a regular expression for checking a string, that it has more than 2 symbols length, first symbol should be Alphabetic, last symbol should be '_'.
And how can I uppercase only first symbol? Thank you.
I need a regular expression for checking a string, that it has more than 2 symbols length, first symbol should be Alphabetic, last symbol should be '_'.
And how can I uppercase only first symbol? Thank you.
Try this :
/^[a-zA-Z].+_$/
Note:
instead of .+
you could use [some allowed chars]+
To match an input with at least 3 char with first being uppercase alphabet and last begin underscore use:
^[A-Z].+_$
To allow any alphabet in the beginning you can use:
^[A-Za-z].+_$
To match
preg_match( "/^[a-z].+_$/i", $str );
To uppercase the first letter
ucfirst( $str );
You could also do it like this (might save further regular expression dithering down the line):
if ((strlen($str) < 3) || !ctype_alpha($str[0]) || (strrchr($str, '_') != '_')) {
echo 'Invalid string';
} else {
$str = ucfirst($str);
}