开发者

php regex for checking a string

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 u开发者_运维技巧ppercase only first symbol? Thank you.


To match

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

To uppercase the first letter

ucfirst( $str );


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].+_$


Try this :

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

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜