I have the following regex, which I interpret as a pattern that will match a string consisting of just alphanumeric characters and hyphens.
if (preg_match(\'/[A
Your pattern matches an ASCII letter or digit or -
anywhere inside a string, and if found, returns true. E.g., it will return true if you pass $#$%$&^$g
to it.
A pattern that will match a string consisting of just alphanumeric characters and hyphens is
if (preg_match('/^[A-Za-z0-9-]+$/D', $subdomain)) {
// valid subdomain
}
Details:
^
- start of string[A-Za-z0-9-]+
- 1 or more chars that are either ASCII letters, digits or -
$
- end of string/D
- a PCRE_DOLLAR_ENDONLY modifier that makes the $
anchor match at the very end of the string (excluding the position before the final newline in the string).