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\"
\w\- is probably the best but here just another alternative Use [:alnum:]
\w\-
[:alnum:]
if(!preg_match("/[^[:alnum:]\-_]/",$str)) echo "valid";
demo1 | demo2