Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match Polish characters in PHP with preg_match

I am trying to do some server side validation in PHP. I tried hard but I found still no solution. I am trying to allow only Polish characters in the input.

For this I have used:

preg_match('/^[\x{0104}-\x{017c}]*$/u',$titles)

This doesn't work however.

Anyone has any idea how to write it properly?

like image 887
user2779917 Avatar asked Dec 06 '25 06:12

user2779917


1 Answers

To match Polish letters only, you just need a character class:

[a-pr-uwy-zA-PR-UWY-ZąćęłńóśźżĄĆĘŁŃÓŚŹŻ]

Use as

preg_match('/^[A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ]*$/iu',$titles)

Note that there is no Q, V and X in Polish, but since they can be met in some words (taxi), you may want to allow these letters as well. Then, use '/^[A-ZĄĆĘŁŃÓŚŹŻ]*$/iu' regex.

IDEONE demo

if (preg_match('/^[A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ]*$/iu', "spółka")) {
    echo "The whole string contains only Polish letters";
}
like image 85
Wiktor Stribiżew Avatar answered Dec 08 '25 18:12

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!