4

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  )
Ali Güzel
  • 85
  • 12

1 Answers1

4

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563