简体   繁体   English

PHP Regex 验证字母和西班牙口音

[英]PHP Regex validate letters and Spanish accent

How can I add/improvised my code so Spanish accent will be considered as valid in addition to normal alphabet (az)我如何添加/即兴修改我的代码,以便除了普通字母 (az) 之外,西班牙口音也被认为是有效的

I have the following in my code我的代码中有以下内容

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

As found in the answer to this question , you could match accented characters using the full Letter Unicode property \\p{L} .正如在这个问题的答案中发现的那样,您可以使用完整的Letter Unicode 属性\\p{L}匹配重音字符。 That includes regular az characters along with accented ones, so replace az in your expression with this like so:这包括常规az字符和重音字符,因此将表达式中的az替换为如下所示:

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

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 ".请注意,要使用它,您需要在结束分隔符之后使用UTF-8 修饰符u ,因为文档说这样的 Unicode“选择UTF-8 模式时可以使用字符类型”。

I think you can use the hexadecimal representation of each letter:我认为您可以使用每个字母的十六进制表示

<?php
function IsAlpha($sString)
{

  $reg = "#[a-zA-Z\xE1\xE9\xED\xF3\xFA\xC1\xC9\xCD\xD3\xDA\xF1\xD1]+#i";
  $count = preg_match_all($reg, $sString, $matches, PREG_OFFSET_CAPTURE);
  return $matches;
}
echo '<pre>';
print_r(IsAlpha("abcdefgh ñ á é í ño 123 asd asáéío nmas asllsdasd óúfl ÑABDJÓ ÚñÍÉ"));
echo '</pre>';

I do not know if there are all the letters, but you can add more with this link .我不知道是否有所有字母,但您可以通过此链接添加更多字母。

您还可以使用T-Regx

pattern('[^\p{L}\s-]', 'u')->fails($s);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM