views:

64

answers:

4

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.

+3  A: 

Try this :

/^[a-zA-Z].+_$/

Note: instead of .+ you could use [some allowed chars]+

M42
+2  A: 

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].+_$
codaddict
+3  A: 

To match

preg_match( "/^[a-z].+_$/i", $str );

To uppercase the first letter

ucfirst( $str );
BBonifield
+1  A: 

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);
}
GZipp