How can I detect non-western characters?

后端 未结 1 1351
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 20:28

I want to disallow certain UTF-8 input (server-side), e.g. eastern languages, where example input might be \" 伊 \".

However, I do want to continue supporting other l

相关标签:
1条回答
  • 2020-12-14 20:45

    Just do

    preg_match('/[^\\p{Common}\\p{Latin}]/u', $string)
    

    where $string is an UTF-8 string. This will return "1" if there are non-latin characters and will return "0" otherwise.

    Example:

    var_dump(preg_match('/[^\\p{Common}\\p{Latin}]/u', 'sf..ŷaás??'));  //int(0)
    var_dump(preg_match('/[^\\p{Common}\\p{Latin}]/u', 'sf..ŷݤaás??')); //int(1)
    
    0 讨论(0)
提交回复
热议问题