rosen_
rosen_

Reputation: 248

Function to check if the first character of a string is a letter

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?

Upvotes: 3

Views: 2133

Answers (2)

amustill
amustill

Reputation: 5302

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

Upvotes: 1

jeroen
jeroen

Reputation: 91742

You just need to remove the comma:

'/^\s*[a-zA-Z]/'

Upvotes: 7

Related Questions