Im trying to set active classes in a nav using php. Now I need to set it by directory rather than complete URL as I have a main landing page for each directory with sub navigation for the other pages within the directory. I was going to use the following but this returns the full url.

function curPageURL() {
$pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
} else {

return $pageURL;}

 function activeLink($pageID) {
if ( $pageID == curPageURL() ) {
    echo 'class="active"';
} }

Then I call activeLink() in a nav item like this:

<li class="projectchild padding1"><a href="" <?php activelink(''); ?> >nexus</a></li>

Your suggestions on how to modify this to return the directory instead would be much appreciated.

Accepted Answer

If you want to extract the directory name from an URL you can use parse_url and dirname as:

$url = '';
$arr = parse_url($url);
$path = dirname($arr['path']); // $path is now /nexus
Written by codaddict
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the community.
It is licensed under cc-wiki