PHP preg_match - only allow alphanumeric strings and - _ characters

后端 未结 5 2044
醉话见心
醉话见心 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:12

    \w\- is probably the best but here just another alternative
    Use [:alnum:]

    if(!preg_match("/[^[:alnum:]\-_]/",$str)) echo "valid";
    

    demo1 | demo2

提交回复
热议问题