PHP正则表达式验证字母和西班牙语口音 [英] PHP Regex validate letters and Spanish accent

查看:447
本文介绍了PHP正则表达式验证字母和西班牙语口音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加/即兴我的代码,所以除了正常的字母(az)之外,西班牙语将被视为有效。

How can I add/improvised my code so Spanish accent will be considered as valid in addition to normal alphabet (a-z)

我的代码中有以下内容

public static function IsAlpha($s){
  $reg = "#[^a-z\s-]#i";
  $count = preg_match($reg, $s, $matches);
  return $count == 0;
}


推荐答案

此问题,您可以匹配重音字符使用完整的 Unicode属性 \p {L} 。这包括常规的 az 字符和重音符,所以用这样替换你的表达式中的 az

As found in the answer to this question, you could match accented characters using the full Letter Unicode property \p{L}. That includes regular a-z characters along with accented ones, so replace a-z in your expression with this like so:

$reg = "#[^\p{L}\s-]#u";

请注意,要使用这个,您需要 UTF-8修饰符 u 在您的关闭分隔符后,作为当选择 UTF-8模式 时,docs会显示这样的Unicode字符类型。

Note that to use this you need the UTF-8 modifier u after your closing delimiter, as the docs say such Unicode "character types are available when UTF-8 mode is selected".

这篇关于PHP正则表达式验证字母和西班牙语口音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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