I made this function to check if the first character is a letter.
我做了这个函数来检查第一个字符是否是一个字母。
function isLetter($string) {
return preg_match('/^\s*[a-z,A-Z]/', $string) > 0;
}
However, if I check a sentence that starts with a coma (,
) the functions returns true
. What is the proper regex to check if the first letter is a-z or A-Z?
但是,如果我检查以逗号(,)开头的句子,则函数返回true。检查第一个字母是a-z还是A-Z的正确正则表达式是什么?
2 个解决方案
#1
5
You just need to remove the comma:
你只需要删除逗号:
'/^\s*[a-zA-Z]/'
#2
1
A slightly cleaner way, in my opinion. Just makes the code a little more human readable.
在我看来,这是一种稍微清洁的方式。只是让代码更具人性化。
function isLetter($string) {
return preg_match('/^[a-z]/i', trim($string));
}
#1
5
You just need to remove the comma:
你只需要删除逗号:
'/^\s*[a-zA-Z]/'
#2
1
A slightly cleaner way, in my opinion. Just makes the code a little more human readable.
在我看来,这是一种稍微清洁的方式。只是让代码更具人性化。
function isLetter($string) {
return preg_match('/^[a-z]/i', trim($string));
}