PHP preg_match - only allow alphanumeric strings and - _ characters

后端 未结 5 2060
醉话见心
醉话见心 2020-12-04 15:50

I need the regex to check if a string only contains numbers, letters, hyphens or underscore

$string1 = \"This is a string*\";
$string2 = \"this_is-a-string\"         


        
5条回答
  •  不知归路
    2020-12-04 16:20

    Why to use regex? PHP has some built in functionality to do that

    
    

    preg_match('/\s/',$username) will check for blank space

    !ctype_alnum(str_replace($valid_symbols, '', $string1)) will check for valid_symbols

提交回复
热议问题