phpregexarabic

How can i allow only arabic characters in input text field?


I already searched here and found similar posts related to this post but i did not find a solution yet .

i tried this :

$text = "الحمد لله رب العالمين hello";

echo $is_arabic = preg_match('/\p{Arabic}/u', $text);

I add the unicode flag but if i add any English characters it is returning true ! any fix for this ?

Any idea folks ?

Thanks in advance


Solution

  • Use unicode flag:

    $text = "الحمد لله رب العالمين";
    echo $is_arabic = preg_match('/\p{Arabic}/u', $text);
                                       here __^
    

    If you want to match only arabic you should do:

    echo $is_arabic = preg_match('/^[\s\p{Arabic}]+$/u', $text);