Forum Moderators: coopster

Message Too Old, No Replies

Getting relative and absolute URL

My code works, but could it be optimized with regular expressions?

         

asantos

6:30 am on Mar 13, 2006 (gmt 0)

10+ Year Member



Hi, i'm developing a CMS, this is how I define the absolute and relative paths of the /index.php file:

# ABSOLUTE PATH
define('ABS', substr(dirname(__FILE__),0,strlen(dirname(__FILE__))-strlen((basename(dirname(__FILE__))))));

# RELATIVE PATH
define('REL', str_replace($_SERVER['DOCUMENT_ROOT'],'',str_replace('\\','/',PATH)));

... i don't know much about regular expressions, is there a way to optimize this constants?

coopster

2:44 pm on Mar 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hi asantos and welcome to WebmasterWorld.

If your code is working and working well without performance issues there is usually not much to be concerned about. However, in this case we might be able to help optimize a bit, who knows ;)

First, you have a CONSTANT named PATH. What is in PATH? Also, the two CONSTANTS you are defining, ABS and REL -- Absolute and Relative to what, the system root or the DocumentRoot?

asantos

5:49 pm on Mar 13, 2006 (gmt 0)

10+ Year Member



coopster, im sorry, I meant:

# ABSOLUTE PATH
define('ABS', substr(dirname(__FILE__),0,strlen(dirname(__FILE__))-strlen((basename(dirname(__FILE__))))));

# RELATIVE PATH
define('REL', str_replace($_SERVER['DOCUMENT_ROOT'],'',str_replace('\\','/',ABS)));

REL resolves through ABS, and ABS is directly related to the current docuement root, in this example is:
c:\mywebsite\index.php

And REL resolves:
[localhost...]

But i need a way to optimize those str_replacements with a preg_replace. how?