PHP preg_match - only allow alphanumeric strings and - _ characters

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

    if(!preg_match('/^[\w-]+$/', $string1)) {
       echo "String 1 not acceptable acceptable";
       // String2 acceptable
    }
    

提交回复
热议问题