本文介绍了使用preg_match()函数的阿拉伯字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用preg_match()来检查阿拉伯文和英文字符,
我使用了这段代码:
preg_match(/ ^ [a-zA-Zç-í] * $ /,$ name)
但仍然不起作用。
编辑:不起作用的代码:
if($ name ==''|| $ email ==''){
echo(< div class = \error \>填写所有栏位< / div>);
}
else if(!preg_match(/ ^ [a-zA-Zأ-ي\\s] * $ /,$ name)){
echo(< div class = \error \>只允许使用字母和空格< / 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
必须替换为正确计算Unicode字符的数量:
echo(< div class = \error \>只有字母并允许空格< / div>);
else if(mb_strlen($ name)< 6){
echo(< div class = \error \> NONONONO les than 6< / div> ;);
}
I am trying to use preg_match() to check Arabic and English characters only,
i used this code:
preg_match("/^[a-zA-Zأ-ي]*$/",$name)
but still, it is not working.
EDIT: The code that does not work:
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 {}
解决方案
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()函数的阿拉伯字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!