title: Extract part from URL for a query string
body: <p>I need a certain part of a URL extracted.</p>
  
  <p>Example:</p>
  
  <p><code>http://www.domain.com/blog/entry-title/?standalone=1</code> is the given URL.</p>
  
  <p><code>blog/entry-title</code> should be extracted.</p>
  
  <p>However, the extraction should also work with <code>http://www.domain.com/index.php/blog/[…]</code>as the given URL.</p>
  
  <p>This code is for a Content Management System.</p>
  
  <hr>
  
  <p>What I've already come up with is this:</p>
  
  <pre><code>function getPathUrl() {
  
      $folder = explode('/', $_SERVER['SCRIPT_NAME']);
      $script_filename = pathinfo($_SERVER['SCRIPT_NAME']); // supposed to be 'index.php'
      $request = explode('/', $_SERVER['REQUEST_URI']);
  
      // first element is always ""
      array_shift($folder);
      array_shift($request);
  
      // now it's only the request url. filtered out containing folders and 'index.php'.
      $final_request = array_diff($request, array_intersect($folder, $request));
  
      // the indexes are mangled up in a strange way. turn 'em back
      $final_request = array_values($final_request);
  
      // remove empty elements in array (caused by superfluent slashes, for instance)
      array_clean($final_request);
  
      // make a string out of the array
      $final_request = implode('/', $final_request);
  
      if ($_SERVER['QUERY_STRING'] || substr($final_request, -1) == '?') {
          $final_request = substr($final_request, 0, - strlen($_SERVER['QUERY_STRING']) - 1);
      }
  
      return $final_request;
  
  }
  </code></pre>
  
  <p>However, this code does not take care of the arguments at the end of the URL (like <code>?standalone=1</code>). It works for anchors (<code>#read-more</code>), though.</p>
  
  <p>Thanks a ton guys and have fun twisting your brains. Maybe we can do this shit with a regular expression.</p>