使用 preg_match() 函数的阿拉伯字符 [英] Arabic characters using preg_match() function

查看:28
本文介绍了使用 preg_match() 函数的阿拉伯字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 preg_match() 仅检查阿拉伯语和英语字符,

I am trying to use preg_match() to check Arabic and English characters only,

我使用了这个代码:

preg_match("/^[a-zA-Zأ-ي]*$/",$name)

但是还是不行.

不起作用的代码:

if($name == '' || $email =='') {
    echo("<div class=\"error\">fill all fields</div>");
}
else if (!preg_match("/^[a-zA-Zأ-ي\s]*$/",$name)) {
     echo("<div class=\"error\">Only letters and white space allowed</div>");
}
else if (strlen($name) < 6) {
    echo("<div class=\"error\">NONONONO les than 6</div>");
}
else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo("<div class=\"error\">WORNG EMAIL</div>"); 
} else {}

推荐答案

您需要使用 /u 修饰符和 preg_match 来确保模式和字符串得到处理作为 Unicode 字符串,strlen 必须替换为 mb_strlen 正确计算 Unicode 字符数:

You need to use a /u modifier with preg_match to make sure the pattern and string are treated as Unicode strings and the strlen must be replaced with mb_strlen to correctly count Unicode characters:

else if (!preg_match("/^[a-zA-Zأ-ي\s]*$/u",$name)) {
     echo("<div class=\"error\">Only letters and white space allowed</div>");
}
else if (mb_strlen($name) < 6) {
    echo("<div class=\"error\">NONONONO les than 6</div>");
}

这篇关于使用 preg_match() 函数的阿拉伯字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆