Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Check alphabetic characters from any latin-based language?

Tags:

Using PHP I want to check that a string contains only alphabetic characters (I do not want to allow any numerals or special characters like !@#$%^&*). ctype_alpha() would seem great for this purpose.

The problem is that I want to allow accented letters, such as found in French, etc. For example, I want to allow "Lórien".

I know that ctype_alpha() can be used with set_locale(), but that still seems too limited for this use case, since I want to allow characters from all latin-based languages.

Any ideas how best to accomplish this?


Note: The solution posted at How can I detect non-western characters? is great for explicitly detecting non-Latin characters, but it allows special characters and white space, which I do not want to allow:

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

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

like image 901
Jordan Magnuson Avatar asked Feb 07 '14 17:02

Jordan Magnuson


1 Answers

How about this regex:

^\p{Latin}+$

Working regex example:

https://regex101.com/r/I5b2mC/1

like image 110
Bryan Elliott Avatar answered Sep 27 '22 21:09

Bryan Elliott