PHP check string has contains only letters numbers and spaces in any language

Viewed 484

How can i check string has contains only letters numbers and spaces in any language?

I have tried bellow but it's not checks special characters like -*/.

 preg_match("/[\p{L}]/u", $string )
 preg_match("/[\p{N}]/u", $string  )
1 Answers

You can use

preg_match('~^[\p{L}\p{N}\s]+$~uD', $string)

Details

  • ^ - start of string
  • [\p{L}\p{N}\s]+ - one or more letters, digits or whitespaces
  • $ - end of string.

The u modifier will enable Unicode string parsing and also make all patterns in the regex Unicode aware, and D will make $ match the very end of the string.

Related