I often find myself parsing a url or filepath for includes and links, and it usually leads to messy, frustrating code. Given PHP's massive library, I assumed there would be something to make life easier, but I couldn't find anything while pouring over the online reference. It's a lot of material, though, so I may have missed something. Does anyone know the best way to play with paths?
Here's one example of this problem. I have a navigation template that I include in every page on my site, but the relative links need to be processed to work at different folder depths. For instance, the relative link to mysite.com/folder1/ from mysite.com/ would be "folder1/". From mysite.com/folder2/, it would be "../folder1/". From mysite.com/folder1/page.html, it would be "/".
The script I wrote to perform this function is pretty messy. It involves grabbing the path through $_SERVER, exploding it around "/", finding the depth of current file, and finally adding the necessary "../" to the relative link. I run into situations like this a lot, and I haven't been able to find a general solution thus far. Any tips on the best way to handing path parsing?