title: How can I get the current web directory from the URL?
body: <p>If I have a URL that is <a href="http://www.example.com/sites/dir/index.html" rel="nofollow noreferrer">http://www.example.com/sites/dir/index.html</a>, I would want to extract the word "sites". I know I have to use regular expressions but for some reason my knowledge of them is not working on PHP.</p>
  
  <p>I am trying to use :</p>
  
  <pre><code> $URL = $_SERVER["REQUEST_URI"];
   preg_match("%^/(.*)/%", $URL, $matches);
  </code></pre>
  
  <p>But I must be doing something wrong. I would also like it to have a catch function where if it is at the main site, www.example.com then it would do the word "MAIN"</p>
  
  <p>Edit: sorry, I've known about dirname...It gives the full directory path. I only want the first directory....   So if its www.example.com/1/2/3/4/5/index.html   then it returns just 1, not /1/2/3/4/5/</p>