Reputation: 131
I would like to highlight the current tab using the url
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$gallerypageurl="http://www.domain.com/gallery/";
if ($gallerypageurl==$url){ echo "selected"; }else { echo "";}
This highlights the gallery tab when the url is http://www.domain.com/gallery/ but my gallery has a pagination in that, so when I go to second page the url is http://www.domain.com/gallery/2/.
This won't highlight the current gallery tab because the $url (http://www.domain.com/gallery/2/) is not equal to $gallerypageurl (http://www.domain.com/gallery/).
How do you extract only the page url without the page number /2/ or /3/
Upvotes: 1
Views: 72
Reputation: 50688
You may split the request uri into array using explode function, then check the array keys are the ones you need to:
$uri = 'http://www.domain.com/gallery/2';
$parts = explode('/', $uri);
echo 'gallery' === $parts[3] ? 'selected' : '';
Call array_shift
twice before, to skip the http
and domain name and have array indexed from 1.
BTW, you should sanitize the $_SERVER
keys before further processing.
Upvotes: 0
Reputation: 5072
This function works only if section's name is in the first segment of the URL path.
function inSection($section, $url)
{
$path = parse_url($url, PHP_URL_PATH);
if(preg_match('@^/([^/]+)/?@', $path, $matches)) {
return $matches[1] === $section;
}
return false;
}
echo inSection('gallery', 'http://www.domain.com/gallery/2/')
? 'selected' : '';
Upvotes: 0
Reputation: 510
You can do it with
if (strpos($url, $gallerypageurl) !== false) {
echo 'selected';
}
Upvotes: 1