0

I want to check for string that contains only english alphabets , digits and symbols.I tried below code but it works only when all the characters are different language.

if(strlen($string) != mb_strlen($string, 'utf-8'))
    { 
 echo "No English words ";
     }
    else {
echo "only english words"; 
   }

For example

1. hellow hi 123#!@#!@#()#@# -- true
2. ព្រាប សុវ ok yes #@# - false
3. this is good 123 - true
4. ព្រាប -- false

p.s : my question is not duplicate because other questions only cover alphabets and symbols , mine covers symbol too

Vishnu
  • 2,372
  • 6
  • 36
  • 58
  • 1
    https://stackoverflow.com/questions/9351306/how-to-check-if-a-php-string-contains-only-english-letters-and-digits this should be your case –  Jun 01 '17 at 08:03
  • Possible duplicate of [How to check, if a php string contains only english letters and digits?](https://stackoverflow.com/questions/9351306/how-to-check-if-a-php-string-contains-only-english-letters-and-digits) – LF00 Jun 01 '17 at 08:04
  • @IvanFranchi and kris : it does not cover symbols – Vishnu Jun 01 '17 at 08:07
  • Depending on the text encoding, you can use `[\x00-\x7f]` or `[\u0000-\u007f]`. – Phylogenesis Jun 01 '17 at 09:17

1 Answers1

2

Would determining if a string is just printable ASCII work? If so you can use this regex:

[ -~]

http://www.catonmat.net/blog/my-favorite-regex/

If you need non ASCII characters as well than you can use the Wikipedia page to get the specific unicode formats that you need:

https://en.wikipedia.org/wiki/List_of_Unicode_characters#Control_codes

MikeBergerUS
  • 196
  • 12