9

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 !@#$%^&).

2
  • 1
    Does this help? Feb 7, 2014 at 17:12
  • @AmalMurali : Thanks. Please see my updated question. Feb 7, 2014 at 17:37

2 Answers 2

11

How about this regex:

^\p{Latin}+$

Working regex example:

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

3
  • Is A-z already contained in \p{Latin}? Don't know, just wondering.
    – user557597
    Feb 7, 2014 at 17:45
  • 1
    Beautiful. This is exactly what I needed. And indeed, ^[\p{Latin}]+$ seems to work just as well... I had no idea that the solution was so simple... Feb 7, 2014 at 18:07
  • 1
    Also in my case it was essential to use the /u (unicode) flag to properly handle UTF-8 encoding. Otherwise it (sometimes) breaks inexplicably and it's really easy to forget about this flag.
    – Rav
    Jul 16, 2017 at 20:56
3

This might work

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

Its all latin, but not punctuation nor whitespace.
where \P means NOT this property
and \p means this property.

Put it in a negative class its

NOT, NOT Latin = Include All Latin
NOT Punctuation = Exclude Punctuation
NOT Whitespace = Exclude Whitespace

2
  • It turns out that ^[\p{Latin}]+$ is really all that's needed here... but thanks for this. Feb 7, 2014 at 18:08
  • Ok, I didn't know if Latin included punctuation and whitespac, guess not.
    – user557597
    Feb 7, 2014 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.