Allow only [a-z][A-Z][0-9] in string using PHP

前端 未结 6 1535
栀梦
栀梦 2020-11-28 14:38

How can I get a string that only contains a to z, A to Z, 0 to 9 and some symbols?

6条回答
  •  醉话见心
    2020-11-28 14:59

    A shortcut will be as below also:

    if (preg_match('/^[\w\.]+$/', $str)) {
        echo 'Str is valid and allowed';
    } else
        echo 'Str is invalid';
    

    Here:

    // string only contain the a to z , A to Z, 0 to 9 and _ (underscore)
    \w - matches [a-zA-Z0-9_]+
    

    Hope it helps!

提交回复
热议问题