如何在输入文本字段中只允许阿拉伯字符?

问题描述:

我已经在这里搜索并找到了与此帖子相关的类似帖子,但我还没有找到解决方案.

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

我试过这个:

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

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

我添加了 unicode 标志,但如果我添加任何英文字符,它将返回 true !有什么解决办法吗?

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

大家有什么想法吗?

提前致谢

使用 unicode 标志:

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);