PHP How to Remove Last Part of a Path

PHP How to remove last part of a path

dirname($path)

And this is the documentation.

php remove the last / from the path

$method = ltrim($_SERVER['PATH_INFO'], '/');

Removing part of path in php

You could try this (tested and works as expected):

$path = 'somefolder/foo/haha/lastone';
$parts = explode('/', $path);
array_pop($parts);
$newpath = implode('/', $parts);

$newpath would now contain somefolder/foo/haha.

How to remove last part of url in PHP

Try this:

$url = explode('/', 'http://www.brightknowledge.org/knowledge-bank/media/studying-media/student-media/image_rhcol_thin');
array_pop($url);
echo implode('/', $url);

In PHP delete last folder in the path

You need to pass the variable to the function:

$dirname = 'AAA/BBB/CCC/DDD/test'
remove_directory($dirname);
function remove_directory($dirname) {
rmdir($dirname)
}

Of course, if your function has only one single line, you can simply use the built-in PHP function instead of defining your own that does the same thing.

I strongly suggest you take a few minutes and read the manual page on variable scope.

PHP - remove last part from URL

$url = 'http://example.com/apples/dogs/coffee';
$newurl = dirname($url);

How to remove last element from URL with PHP?

Explode the $_SERVER['SCRIPT_URL'] into an array, remove empty elements, and then remove the last array element. Then, implode it all back together with initial and trailing slashes.

$urlArray = explode('/',$_SERVER['SCRIPT_URL']);
$urlArray = array_filter($urlArray);

// remove last element
array_pop($urlArray);

$urlString = '/'.implode('/',$urlArray).'/';

Don't store array_pop() as a variable, unless you need the last array element.

Remove last child page from URI

you can use this and you can get your required output:

// implode string into array
$url = "http://192.168.0.16/wordpress/blog/page-2/";
//then remove character from right
$url = rtrim($url, '/');
// then explode
$url = explode('/', $url);
// remove the last element and return an array
json_encode(array_pop($url));
// implode again into string
echo implode('/', $url);

another approach is:

// implode string into array
$url = explode('/', 'http://192.168.0.16/wordpress/blog/page-2/');
//The array_filter() function filters the values of an array using a callback function.
$url = array_filter($url);
// remove the last element and return an array
array_pop($url);
// implode again into string
echo implode('/', $url);

PHP - Remove the last directory from a URL

$url = "http://www.example.com/news/media-centre/news/17/an-example-news-post/?foo=bar";
$info = parse_url($url);
$info["path"]=dirname($info["path"]);

$new_url = $info["scheme"]."://".$info["host"].$info["path"];
if(!empty($info["query"])) $new_url .= "?".$info["query"];
if(!empty($info["fragment"])) $new_url .= "#".$info["fragment"];


Related Topics



Leave a reply



Submit