You could use simple, regular explode
& implode
to get what you want as shown by the Snippet below. Quick-Test Here.
<?php
// ASSUMING THAT $_SERVER['REQUEST_URI']; RETURNS THE STRING BELOW:
$uri = /*$_SERVER['REQUEST_URI'];*/ "...../india-hotels/dun-hotels/abc.php";
$parts = explode("/", $uri);
array_pop($parts);
$uri = implode("/" , $parts) . "/";
echo $uri; //<== YIELDS::: ...../india-hotels/dun-hotels/
Alternatively and even preferably, You could use the basename()
Function in combination with str_replace()
to achieve the same goal:
<?php
// ASSUMING THAT $_SERVER['REQUEST_URI']; RETURNS THE STRING BELOW:
$uri = /*$_SERVER['REQUEST_URI'];*/ "...../india-hotels/dun-hotels/abc.php";
$baseName = basename($uri);
$uri = str_replace($baseName, "", $uri);
echo $uri; //<== YIELDS::: ...../india-hotels/dun-hotels/
In a condensed form:
<?php
$uri = /*$_SERVER['REQUEST_URI'];*/ "...../india-hotels/dun-hotels/abc.php";;
$uri = str_replace(basename($uri), "", $uri);
echo $uri; //<== YIELDS::: ...../india-hotels/dun-hotels/