php remove the last "/" from the path - php

this is my code
$method = $_SERVER['PATH_INFO'];
and this is my path:
http://localhost:8082/XXXX/controllers/User.php/newUser?name=hello
the result of the method is /newUser
I would like to have just newUser. IE without the /
could you help me please

use ltrim on the variable you want ? Seems the easiest way to me
$var = ltrim($var,"/");

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

$withoutSlash = substr($_SERVER['PATH_INFO'], 1);

Maybe your question already exist:
URL: PHP How to remove last part of a path
One solution:
preg_replace("/\/\w+$/i","",__DIR__);
# Note you may also need to add .DIRECTORY_SEPARATOR at the end.
Another solution:
dirname($path)
Documentation: http://ca3.php.net/dirname

Related

Remove slash from final string and GET

So earlier I was using http://localhost/profile.php?username=joe but now I'm using htaccess to make the URL look nice, so this is what it now looks like http://localhost/joe. Now here's the issue, before I did this
$username = $_GET['username'];
And then use it to get the username, but now how would I do it? I've seen other posts, but they seem to use longer and more complex URLs.
I can do this
$url = 'http://localhost/joe';
echo parse_url($url, PHP_URL_PATH);
And it seems to work fine, but I get a / before the actual result so I get /joe, and I want joe. I've also tried stripslashes but that didn't seem to work. So any ideas?
Trim the result on the left site to avoid removing or replacing slashes that are part of the actual value.
$result = ltrim($path, '/');
You can also use explode and array_pop.
$url = 'http://localhost/joe';
echo array_pop(explode("/", $url))
Use str_replace(). Like this
$url = 'http://localhost/joe';
$final = parse_url($url, PHP_URL_PATH);
$result = str_replace('/','',$final);
echo $result;
stripslashes() works for backward slashes, not forward slashes
Just a substr call should also work:
$url = 'http://localhost/joe';
echo substr(parse_url($url, PHP_URL_PATH), 1); // joe
You can learn about placeholders in routes. Try this article
Route Parameters
Route parameters can be used to insert placeholders into your route definition. This will effectively create a pattern in which URI segments can be collected and passed to the application’s logic handler.
This might sound a little confusing, but when you see it in action everything will fall into place. Here we go...
<?php
// app/routes.php
// routes for the books section
Route::get('/books', function()
{
return 'Books index.';
});
Route::get('/books/{genre}', function($genre)
{
return "Books in the {$genre} category.";
});
Also there is basename()
echo basename('http://localhost/joe'); //joe
It's better to use this rule or mod proxy to rewrite REQUEST_URI:
RewriteCond %{REQUEST_URI}
Then you can get your parameter and remove its slashes easily with trim($_SERVER['REQUEST_URI'],'/').

Small issue with str_replace

This is my code:
$produrl = '/'. basename(str_replace(".html","",$_cat->getUrl())) .'/' . basename($_product->getProductUrl()) ;
$_cat->getUrl() returns this:
http://website.com/category/subcategory.html
I need: for $produrl part of basename(str_replace(".html","",$_cat->getUrl()))to return category/subcategory
The issue:
It gives back only category without /subcategory I know the issue is in the str_replace it's wrong isn't? Can you help me with that?
Pleast Note I have 2 Cases:
Sometimes I get http://website.com/category/subcategory.html.
Sometimes http://website.com/category.html
And I would like it to work with both of them :)
You should use parse_url instead:
$url = parse_url($_cat->getUrl());
$produrl = str_replace(".html","",$url['path']);
The problem is that you're using the basename() function.
Take a look at the documentation for it: http://php.net/manual/en/function.basename.php
It will only return the trailing string after the "/" (forward slash) or "\" (back slash, for Windows).

PHP: Get last directory name from path

I am writing one function for getting some different database query. Now things are going well but only need to get last directory name from defined path.
$qa_path=site_root('/learnphp/docs/');
I wan to get only docs from above path. Here site_root is nothing but $_SERVER['DOCUMENT_ROOT'] So how can I get only docs ?
Thanks
Easiest way would be to use basename($yourpath) as you can see here: http://php.net/basename
Provided answer doesn't work if your string contains the file at the end, like :
basename('/home/mypath/test.zip');
gives
test.zip
So if your string contains the file, don't forget to dirname it first
basename(dirname('/home/mypath/test.zip'));
gives
mypath
This is the easiest way:
<?php
echo basename(getcwd());
?>
getcwd() = give your full directory path
basename() = give you last directory
Try explode('/', '/learnphp/docs/')
to split the string into array locations. Then fetch the last location.
Here is more info:
http://php.net/manual/en/function.explode.php
you can use this simple snippet:
$qa_path=site_root('/learnphp/docs/');
$qa_path = explode("/", $qa_path);
$qa_path = $qa_path[count($qa_path) - 1];
$qa_path=explode('/', '/learnphp/docs/');
echo $qa_path[2]; // output docs
This will help you
$qa_path=site_root('/learnphp/docs/');
$q_path = explode ("/", $qa_path);
$lastV = end($q_path);
This gives you the current directory name:
echo basename(dirname(__FILE__));

how to view only file name by $_SERVER['PHP_SELF'] and not GET values appended with it

In one of my page image.php?id=somenumber , I am using <?php echo $_SERVER['PHP_SELF']; ?> to recognize this page in the common sidebar. But using this $_SERVER['PHP_SELF'] yields also the value of id as image.php?id=32 which I don't want.
How do I only get the filename?
$_SERVER['SCRIPT_NAME'], if you are using Apache at least, should cover what you need. It should be the executed script without the query string and relative to the document root (as opposed to SCRIPT_FILENAME, REQUEST_URI, or PHP_SELF.
$parts = explode('/', $_SERVER["SCRIPT_NAME"]);
$file = $parts[count($parts) - 1];
You can simply use this to cut off the query string:
$_SERVER['SELF']=array_shift(explode('?',$_SERVER['SELF']));
echo parse_url('http://dummy.com/'.$_SERVER['SELF'], PHP_URL_PATH);
The http://dummy.com/ part is only there because parse_url can't work with relative urls.
What version of PHP are you using? Mine (5.3.3) doesn't have $_SERVER['SELF'].
$_SERVER['PHP_SELF'] though gives the script name (image.php).
$fileName=explode("/",$_SERVER['SCRIPT_NAME']);
echo end($fileName);

regexp get filename from url

url: http://blabla.bl/blabla/ss/sd/filename
How to get a filename?
http://us2.php.net/manual/en/function.basename.php
It works even on urls.
Use earcar's suggestion (basename) to get the filename.
BUT, if we're starting with a URL and the filename includes a query string, use Mauris's suggestion as well.
The query string will start with ? (that's how we know it's not part of the filename) and we can use
explode('?', basename($url));
This is summed up by the online PHP manual for basename
Given an arbitrary URL, I think you should use basename() together with parse_url(). Something like this:
$parsed_url = parse_url($url);
$path = $parsed_url['path'];
$filename = basename($path);
Yes it works, but last thing, sometimes basename is aaaa_somewhat. How to delete _somewhat?

Categories