0

I have a few strings like these:

$str = 'this is a test 1';
$str = 'this is a test س';
$str = 'this is a test!';
$str = 'this is a test';

And I want this output:

false // there is this number: `1`
false // there is a Arabic character: س
false // there is a exclamation mark
true // that's pure English

As you see, I need to determine a string is pure-English or not. How can I do that?

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89

1 Answers1

2

Assuming "pure English" = English letters + space:

^[a-zA-Z ]*$
  • [a-zA-Z ] - defines a character set containing lower and upper case letters + space
  • * - repeat any number of times
  • ^$ - make sure the string is matched from the start (^) til the end ($)
ndnenkov
  • 35,425
  • 9
  • 72
  • 104
  • Just for my information, How can I add all symbols *(`!`, `.`, `@`, `#`, `^`, `%` and etc ..)* to it? – Shafizadeh Dec 26 '15 at 22:48
  • @Shafizadeh, you can add them between the brackets. However, be careful - some characters have special meanings. For example don't put the `^` at the beginning of the brackets, because then it would have the meaning of negation. – ndnenkov Dec 26 '15 at 22:51