验证输入作为阿拉伯字符的名称

时间:2015-07-31 09:17:18

标签: php regex string

我有一个输入,我需要检查它只是字母字符(作为名称,例如彼得)。这是我的代码:(但它只适用于英文字符)

$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  $nameErr = "Only letters and white space allowed"; 
}

如何为阿拉伯字符创建regex

2 个答案:

答案 0 :(得分:2)

只需使用\p{L}即可匹配任何语言的任何字母。

$name = test_input($_POST["name"]);
if (!preg_match("/^[\p{L} ]+$/u",$name)) {
  $nameErr = "Only letters and white space allowed"; 
}

如果您不想要,请从角色类中删除空格字符。

答案 1 :(得分:1)

preg_match("/\p{Arabic}/u", $name)

应该为你做