3

In PHP, how do I check if a String contains only letters? I want to write an if statement that will return false if there is (white space, number, symbol) or anything else other than a-z and A-Z.

My string must contain ONLY letters.

I thought I could do it this way, but I'm doing it wrong:

if( ereg("[a-zA-Z]+", $myString))
   return true;
else
   return false;

How do I find out if myString contains only letters?

2
  • 11
    That's not Java. You probably meant PHP. Commented Apr 24, 2010 at 8:47
  • Why regex? ctype_alpha($myString); Commented Jan 25, 2017 at 16:26

6 Answers 6

10

Yeah this works fine. Thanks

if(myString.matches("^[a-zA-Z]+$"))
Sign up to request clarification or add additional context in comments.

Comments

9

Never heard of ereg, but I'd guess that it will match on substrings.

In that case, you want to include anchors on either end of your regexp so as to force a match on the whole string:

"^[a-zA-Z]+$"

Also, you could simplify your function to read

return ereg("^[a-zA-Z]+$", $myString);

because the if to return true or false from what's already a boolean is redundant.


Alternatively, you could match on any character that's not a letter, and return the complement of the result:

return !ereg("[^a-zA-Z]", $myString);

Note the ^ at the beginning of the character set, which inverts it. Also note that you no longer need the + after it, as a single "bad" character will cause a match.


Finally... this advice is for Java because you have a Java tag on your question. But the $ in $myString makes it look like you're dealing with, maybe Perl or PHP? Some clarification might help.

1 Comment

Note that ereg as been deprecated as of PHP 5.3. Use preg_match instead
4

Your code looks like PHP. It would return true if the string has a letter in it. To make sure the string has only letters you need to use the start and end anchors:

In Java you can make use of the matches method of the String class:

boolean hasOnlyLetters(String str) {
   return str.matches("^[a-zA-Z]+$");
}

In PHP the function ereg is deprecated now. You need to use the preg_match as replacement. The PHP equivalent of the above function is:

function hasOnlyLetters($str) {
   return preg_match('/^[a-z]+$/i',$str);
}

Comments

3

I'm going to be different and use Character.isLetter definition of what is a letter.

if (myString.matches("\\p{javaLetter}*"))

Note that this matches more than just [A-Za-z]*.

A character is considered to be a letter if its general category type, provided by Character.getType(ch), is any of the following: UPPERCASE_LETTER, LOWERCASE_LETTER, TITLECASE_LETTER, MODIFIER_LETTER, OTHER_LETTER

Not all letters have case. Many characters are letters but are neither uppercase nor lowercase nor titlecase.

The \p{javaXXX} character classes is defined in Pattern API.

Comments

0

Alternatively, try checking if it contains anything other than letters: [^A-Za-z]

Comments

0

The easiest way to do a "is ALL characters of a given type" is to check if ANY character is NOT of the type.

So if \W denotes a non-character, then just check for one of those.

1 Comment

True in general, but in Java it's usually easier to do it the other way because of the implicit anchoring of the matches() method. So it's s.matches("\\w+") vs !s.matches(".*\\W.*")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.