PHP: check alphabetical characters in any latin language?

Using PHP I want to verify that the string contains only alphabetic characters (I do not want to allow any numbers or special characters such as! @ # $% ^ & *). ctype_alpha()would seem great for this purpose.

The problem is that I want to allow accented letters, for example, in French, etc. For example, I want to enable "Lรณrien".

I know what ctype_alpha()you can use with set_locale(), but it still seems too limited for this use case, since I want to allow characters from all Latin languages.

Any ideas how best to do this?


Note. The solution is published in How can I detect non-Western characters? great for explicit detection of non-latin characters, but it allows you to use special characters and spaces, which I don't want to allow:

preg_match('/[^\\p{Common}\\p{Latin}]/u', $string)

I want something that will work this way, but limit the allowed characters to alphabetic characters (so no special characters like! @ # $% ^ &).

+3
source share
2 answers

How about this regex:

^[\p{Latin}[A-Za-z]+$

An example of a working expression:

http://regex101.com/r/vN6qU8

+5
source

It can work

 [^\P{latin}\s\p{Punctuation}]

, .
\P
\P .

, NOT Latin =
NOT =
NOT Whitespace =

+3

All Articles