title: PHP regex to match alphanumeric and hyphens (not spaces) for use as subdomain
body: <p>I have the following regex, which I interpret as a pattern that will match a string consisting of just alphanumeric characters and hyphens. </p>
  
  <pre><code>if (preg_match('/[A-Za-z0-9-]/', $subdomain) === false) {
      // valid subdomain
  }
  </code></pre>
  
  <p>The problem is this regex is also matching blank spaces, which is obviously undesirable in a subdomain name. </p>
  
  <p>For bonus points, is this a suitable way to validate subdomain names? I'm unsure which special characters are allowed but thought I would keep it simple by only allowing hyphens. Is there anything else I need to check for? For instance a maximum length? </p>