Accept only UTF8 letters with preg_match

I am trying to resolve Chinese, Japanese (Hiragana, Katakana, Kanji), Korean, and basically any Unicode letter. I would like the first character to be a letter

$pattern = '/\p{L}[\p{L}\p{N} _.-]+/u'; if(!preg_match($pattern, $subuser)){ //Error } 

However, my pattern seems to accept strings with numbers in front. When I added:

 '/^\p{L}[\p{L}\p{N} _.-]+$/u' 

No lines were accepted. I tried using \ p {Hiragana} etc., but without real luck. Does anyone see what I'm doing wrong?

+4
source share
2 answers

This should do the trick:

 <?php $lines = array('12345', 'w123', 'hello'); $valid = array_filter($lines, function($line){ return preg_match('/^\p{L}{1,}/iu', $line); }); var_dump($valid); 
+2
source

The Holy Grail, when it comes to sanitation: http://htmlpurifier.org/ It cleans up all data and skips only utf-8 characters. Some recommended character reading: http://htmlpurifier.org/docs/enduser-utf8.html

+1
source

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


All Articles