Re: help condensing regular expressions
| From: | Chris Adams | Date: | Sat, 06 Apr 2002 05:49:25 +0000 |
| Subject: | Re: help condensing regular expressions | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-91844@lists.php.net to get a copy of this message | ||
In article <5.1.0.14.0.20020406055420.032b6bd8@crux.kwikin.com>, Tom
Rogers wrote:
> I am trying to calculate how far into a directory structure I am so that I
> can include images and include files without having to hard code them.
> I need to turn $PHP_SELF which could be /admin/emails/index.php into ../../
> which I can then use to get to any directory from root.
Here's a more compact regexp:
preg_replace("|/[^/]+|", "../", dirname($_SERVER['PHP_SELF']));
dirname() returns only the directory part of a filename (e.g. "/~chris"
from "/~chris/myfile.php").
Using | instead of the usual / to delimit the regular expression just
saves a bit of typing.
(Note that for includes, you'd be better off setting include_path in
php.ini or .htaccess - that avoids the need to worry about where the
files are physically located)
If you need the local file path and you're using Apache, the
PATH_TRANSLATED variable can tell you where your files are:
$current_dir = dirname($PATH_TRANSLATED) . "/";
This could be used to construct absolute paths internally - I use it in
my photo gallery code when creating thumbnails & such with convert.
Using it with DOCUMENT_ROOT can help if you need to construct things
like directory listings.