1

I am trying to create a regex which will detect if a string has only letters in it. Can anyone tell me if the following code does that correctly?

$text = "asdsad";
if (ereg('[^A-Za-z]', $text)) {
  echo "More then letters";
}

else {
  echo "only letters";    
}
4
  • What is your question? I don't understand your first sentence. Commented Feb 15, 2011 at 1:30
  • 2
    Russell, consider that the person may not have English as their first language. Commented Feb 15, 2011 at 1:45
  • @Steve: It's perfectly acceptable to state that you do not understand the only sentence in the question... Commented Feb 20, 2011 at 11:52
  • @Russell true, but when others are managing to give answers, obviously the question CAN be understood. So if we as a community are going to help people we need to be considerate of those who are learning English, just as we might be considerate of those learning (say) PHP. Commented Feb 28, 2011 at 19:52

2 Answers 2

14

The condition evaluates to true, if that is what you meant.

You want to make sure a string has letters only?

Try this...

if (preg_match('/^\pL+$/u', $text)) {
   echo "Letters only";
} else {
   echo "More than letters";
}

See it on ideone.

So you can understand a few things...

  • ereg() has been deprecated as of PHP 5.3. Stop using it, and use preg_match().
  • When using a regex with preg_match(), you need to specify a regex delimiter. Most regex flavours use /, but PHP lets you use most matching pair of chars, e.g. ~
  • You are missing start and end anchors. This means you won't be matching the string from start to finish
  • Use Unicode regex where available, so non ASCII letters are still detected.

Update

Test cases.

PHP

$testCases = array(
    '',
    'abc',
    '123',
    'لإنجليزية',
    'abc1',
    'русский'
);

foreach($testCases as $str) {
    echo '"' . $str . '" letters only? ' . var_export((bool) preg_match('/^\pL+$/u', $str), TRUE) . "\n";
}

Output

"" letters only? false
"abc" letters only? true
"123" letters only? false
"لإنجليزية" letters only? true
"abc1" letters only? false
"русский" letters only? true
Sign up to request clarification or add additional context in comments.

7 Comments

Shouldn't it be preg_match('/^\pL+$/u', $text)?
I think this might be dependent on the encoding of the source file. I don't know PHP, though...
@Tim Pietzcker Well PHP does have the u modifier, so I don't know. Anyone?
thanks all but its working only with english alpha but realy my language is arabic can some on help plz
@user553786 Check update with new test case of an Arabic string that passes.
|
2

A better solution not using regular expression would be to use ctype_alpha

https://www.php.net/manual/en/function.ctype-alpha.php

As long as using the current locale, not just assuming the alphabet is a-zA-Z, is suitable.

Comments

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.