PHP Regex checks letters and Spanish accent

How can I add / improvise my code, so the Spanish accent will be considered valid in addition to the normal alphabet (az)

I have code in code

public static function IsAlpha($s){ $reg = "#[^az\s-]#i"; $count = preg_match($reg, $s, $matches); return $count == 0; } 
+6
source share
2 answers

As found in the answer to this question , you can match characters with an accent using the full letter Unicode Property \p{L} . This includes the usual az characters along with accented characters, so replace az in your expression as follows:

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

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

+20
source

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 don’t know if all the letters are there, but you can add another link.

+1
source

Source: https://habr.com/ru/post/902113/


All Articles