PetroGromovo's avatar

Why I can not check text in cyrillic ( ukrainian language) ?

I need on laravel 10 site to check that field user_name is only text and in 1 Cyrillic language( ukrainian )

Reading this https://stackoverflow.com/questions/6942239/validating-cyrillic-input-in-php/6943461 branch :

UPDATED :

I made console test with 5 examples : 3 first valid Cyrillic(ukrainian) phrases and last 32 invalid :

public function handle()
{
    $pattern  = "/^[\p{Cyrillic}\s]+$/ui";
    $subjects = array('Петренко Василь', "авф віфв Я", "Алр", 'Петренко Василь98', 'QWERT Петренко Василь');
    foreach($subjects as $subject) {
        $match = (bool) preg_match($pattern, $subject);
        if($match)
            echo "$subject :: YES matches the testing pattern \r\n";
        else
            echo "$subject :: No \r\n";
    }
}

These tests work ok, but when I try to use similar validation in laravel request like :

class CheckNameIsUkrainianAlphabet implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if( ! (new CheckUkrainianAlphabet())->check($value, allowPoint: false) ) {
            $fail('Неправильне ім\'я. Тільки українські символи мають використовуватися.');
        }
    }
}

and in app/Services/Library/CheckUkrainianAlphabet.php :

<?php

namespace App\Services\Library;

use Illuminate\Support\Str;

class CheckUkrainianAlphabet
{
    public function check(string $value, bool $allowPoint): bool
    {
        $pattern = "/^[\p{Cyrillic}\s]+$/ui";
        $l = Str::length($value);
        \Log::info('-1 $value::'.print_r($value, true));

        for ($i = 0; $i < $l; $i++) {
            $ch = $value[$i];
            \Log::info('-2 $ch::'.print_r($ch, true));
            \Log::info('-3 ord($ch)::'.print_r(ord($ch), true));

            $match = (bool) preg_match($pattern, $ch);
            if ( ! $match) {
                \Log::info('-20 INVALID $ch::'.print_r($ch, true));
                return false;
            }
        }

        return true;
    }
}

I see in log output :

[2025-03-12 12:30:38] local.INFO: -1 $value::ÐеÑÑеМкП ÐаÑÐžÐ»Ñ  
[2025-03-12 12:30:38] local.INFO: -2 $ch::Ð  
[2025-03-12 12:30:38] local.INFO: -3 ord($ch)::208  
[2025-03-12 12:30:38] local.INFO: -20 INVALID $ch::Ð

and my tests always return false. I do not understand it works ok in console and this error under request ?

0 likes
0 replies

Please or to participate in this conversation.