RisingSun
RisingSun

Reputation: 1733

PHP regex check if string is latin or cyrillic

I am trying to check if a user input is either in Latin or Cyrillic. I would like to make the user enter a text that is in only Latin or Cyrillic letters. How do I allow one and deny the other? I dont want the user to mix latin and cyrillic. It is only one or the other. I am new to regex and cant figure out a way to do it. Here is what I have so far.

!preg_match("/^([a-zA-Z]+|[\p{Cyrillic}]+)$/u", $inputstr)

Also, What does "/" at the beginning and the end do? And what does "/u" do? Any help is appreciated.

Upvotes: 0

Views: 6218

Answers (1)

Lone Shepherd
Lone Shepherd

Reputation: 1005

The documentation zerkms linked answers some of your questions.

First, what is the / at the beginning and end? That's the delimeter pattern enclosing your regex.

Second, what is the u? That's a modifier to treat the pattern string as unicode.

To allow only one type of char, use grouping, as follows: /^(?:\p{Cyrillic}+|\p{Latin}+)$/u

That should match either Cyrillic chars or Latin chars, but not both in the same string.

(?:stuff) is a grouping subpattern that matches but does not capture.

Upvotes: 3

Related Questions