regex to check the string contains only letter and numbers but not only numbers

后端 未结 10 1465
温柔的废话
温柔的废话 2021-02-06 11:31

I need a help with regex which checks the string contains only letter and numbers but not only numbers

Valid

* letters
* 1wret
* 0123chars
* chars0123
*          


        
10条回答
  •  灰色年华
    2021-02-06 11:56

    Instead of using a regular expression, you can also use the ctype_*() functions:

    var_dump(ctype_alnum('letters') && !ctype_digit('letters'));     // bool(true)
    var_dump(ctype_alnum('0123chars') && !ctype_digit('0123chars')); // bool(true)
    var_dump(ctype_alnum('1324') && !ctype_digit('1324'));           // bool(false)
    var_dump(ctype_alnum('xcvxxc%$#') && !ctype_digit('xcvxxc%$#')); // bool(false)
    

    But if you want a regular expression, you can use this:

    var_dump(preg_match('/^[a-z0-9]*[a-z]+[a-z0-9]*$/i', $input));
    

提交回复
热议问题