Dynamic Relative Paths

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Dynamic Relative Paths

Post by neophyte »

I've been working on a script today to calculate dynamic relative paths.

Code: Select all

//Calculate correct relative paths
if  (substr_count( $_SERVERї'REQUEST_URI'], '/') != 1)
{
$relative_path ='';
for($x=0;$x < substr_count( $_SERVER&#1111;'REQUEST_URI'], '/') -1; $x++)
&#123;
$relative_path .= '../';
&#125;
$link_path = $relative_path;
&#125;
Does anybody know of a better way to do this than what I've done? Is there a built in function I don't know about? Do you think this will work to attach $link_path to the front of my links? It seems to be working in my tests. What do you all think?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

could use str_repeat along with the call to substr_count..
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

You mean something like....

Code: Select all

$link_path = str_repeat ('../' ,(substr_count( $_SERVER&#1111;'REQUEST_URI'], '/') -1));
That's cool Feyd!
8)
Post Reply