title: Strip document root from full server path in PHP
body: <p>OK, I have defined a PHP constant that points to a file on the server like this:</p>
  
  <pre><code>define('SOME_FILE', str_replace('//', '/', str_replace('\\', '/', dirname(__FILE__) . '/foo-bar.txt')));
  </code></pre>
  
  <p><code>SOME_FILE</code> will contain a full path + filename which is useble in PHP functions like <code>include</code>, <code>fopen</code>, etc. Now if I want to use this path as an URL in my (X)HTML output, I'm screwed. To do so, I need to strip off the document root.</p>
  
  <p>Usually, <code>$_SERVER['DOCUMENT_ROOT']</code> is set in 99% of PHP environments, but it can happen where it's poorly configured, overwritten by the user or even missing.</p>
  
  <p>Now what I want to do is to <code>define()</code> another PHP constant based on <code>SOME_FILE</code> that will point to the actual URL of the file (the document root path is stripped off).</p>
  
  <p>I know I can do this in 3-4 lines of code with <code>__FILE__</code>, <code>$_SERVER['SCRIPT_NAME']</code> (SCRIPT_NAME is required as per CGI 1.1 specification but not DOCUMENT_ROOT), <code>strrpos</code>, and <code>substr</code>, but I'm looking for a "one line" solution to use in a define statement...</p>
  
  <p>I use PHP 5.1.0+. Amy suggestions welcome!</p>