php only allow letters, numbers, spaces and specific symbols using pregmatch

后端 未结 3 988

on my php i use preg_match to validate input texts.

if(preg_match(\'/^[a-zA-Z0-9]+$/\', $firstname)) {
}

But this only allows alp

3条回答
  •  庸人自扰
    2021-01-31 17:15

    If you not only want to allow ASCII, then use Unicode properties:

    preg_match('/^[\p{L}\p{N} .-]+$/', $firstname)
    

    \p{L} is any letter in any language, matches also Chinese, Hebrew, Arabic, ... characters.

    \p{N} any kind of numeric character (means also e.g. roman numerals)

    if you want to limit to digits, then use \p{Nd}

提交回复
热议问题