preg_match alphanumeric and hyphens only

Viewed 25

I'm trying to see if a string contains only alphanumeric characters or hyphens

Right now I have this code to check for alphanumeric

if (ctype_alnum($string)) {
    //do something
}

and have tried preg_match, but cant find the right pattern:

preg_match('/[^a-z\-0-9]/i', $string)

How can I check if a string contains only alphanumeric characters or hyphens? I have troved the web for a solution but can't find this specific case and am not very good with regex/preg_match

0 Answers
Related