UTF-8 & IsAlpha() in PHP

后端 未结 4 1145
甜味超标
甜味超标 2020-12-19 21:01

I\'m working on a application which supports several languages and has a functionality in place which tries to use the language requested by the browser and also allows manu

4条回答
  •  我在风中等你
    2020-12-19 21:36

    If you'd like to check only for valid unicode letters regardless of the used language I'd propose to use a regular expression (if your pcre-regex extension is built with unicode support):

    // adjust pattern to your needs
    // $input needs to be UTF-8 encoded
    if (preg_match('/^\p{L}+$/u', $input)) {
        // OK
    } else {
        // not OK
    }
    

    \p{L} checks for unicode characters with the L(etter) property which includes the properties Ll (lower case letter), Lm (modifier letter), Lo (other letter), Lt (title case letter) and Lu (upper case letter) - from: Regular Expression Details).

提交回复
热议问题