ok so im making a file system viewer.
Im stuck on this though, I have everything working fine except when the I click the back button on the gui I have made.
So I have a string like so
http://www.grubber.co.nz/update/index.php?dir=../developer/_social_development
The script will go to the folder /developer/_social_development
but when I want to go back I press the back button and it will go back to the top directory so it goes all the way back to the first '/' for example http://www.grubber.co.nz/update/index.php?dir=../
I use this code to get back to the last page which doesn't work
$dir = $_GET['dir'];
$marker = "/";
echo $str = (substr($dir, 0, (strpos($dir, $marker) + strlen($marker))));
all it does is remove everthing to the first '/' but I want it to goto the last '/' for example it was this /developer/_social_development and when I click on the previous folder i want it to be /developer also the string will change depending on what folder you are in so I cant just remove a set amount of characters
Thanks for the help
Using another method str pos, you may use explode. here complete code:
$dir = '../developer/_social_development';
$marker = "/";
$arrDir = explode('/', $dir);
array_pop($arrDir);
$dir2 = implode( '/', $arrDir);
echo $dir2;
result: ../developer
Instead of using strpos() which gives you the position of the first occurrence of a character in a string you should use strrchr() that will give you the position of the last occurrence of the character in your string
Not a pretty best solution, personally I'd use a RegEx approach, but if you wanted to use str position you could do something like:
$dir = $_GET['dir'];
$marker = "/";
echo $str = strrev(substr(strrev($dir), (strpos($dir, $marker) + strlen($marker))));
Or a RegEx based solution:
$dir = $_GET['dir'];
echo $str = preg_replace("/\/[^\/]+$/", "", $dir);
$path = '/foo/bar';
$path = $path . '/..'; // /foo/bar/..
echo realpath($path); // Shows /foo
Related
My problem is as follows. My PHP code relies on a string to determine a file directory, by removing the last part of the path. I want to go back a directory. Consider the following example:
Original Input:
$dir = "this/is/a/files/location"
Desired Output:
$dir = "this/is/a/files"
Basically, I need it to remove everything to the right of the first "/".
Thank you so much for your help with this!
You have to use substr() along with strrpos()
$dir = "this/is/a/files/location";
$dir = substr($dir, 0, strrpos( $dir, '/'));
Output:- https://eval.in/949366
This Might Help
$dir = "this/is/a/files/location";
$popped = explode('/',$dir);
array_pop($popped);
$dir = implode('/',$popped);
Another method, using str_replace and basename
$dir = "this/is/a/files/location";
echo str_replace( '/' . basename( $dir ), '', $dir );
output:
this/is/a/files
update [ 01.04.2019 ]
Having just been given a plus one I looked again at this and have an additional snippet to offer ~ it seems to me much easier than my previous offering - chdir('../')
/* where did we start off? */
printf('Current directory: %s<br />', getcwd() );
/* go up a level */
chdir('../');
/* where are we now? */
printf('Current directory: %s', getcwd() );
I have this string
../../some/folder/image.png
and it's possible that the string will be
../../../../../../some/folder/image.png
and I want to remove all ../ and add /root/folder/ in front of some/folder/image.png.
How do I do this?
edit
And sometime that string is placed in the middle. I mean it might be like this:
hallo hallo ../../../some/folder/image.png.
You could do the following :
$path = "hello lol ../../some/folder/image.png";
$path = preg_replace("#.*\.\./#", "", $path);
$path = "/root/folder/" . $path;
Depends where you want to use it, but you would do something like this
$path = "../../../../../some/folder/image.png";
$searchFor = substr($path, 0, strpos( $path, "some"));
$path = str_replace($searchFor, "/root/folder/", $path);
I know the preg_replace() is better, but I wanted to share a different answer
My pages are www.example.com/somthing/types.html , www.example.com/somthing2/types.html this html files has a tag<a href="animals.html" . somthing,somthing2are files every file contains links in types.html and files like animals.html in the same folders.
when i get it with dom it shows only "animals.html" but i want to get "www.example.com/somthing/animals.html". how can i remove types.html from www.example.com/somthing/types.html and put www.example.com/somthing+/animals.html.
i just need a way to remove "types.html" from www.example.com/somthing/types.html.
keep the folder and remove the last part after this "/".
i dont know always last part (file name) so i need a way to remove last thing after last "/". sorry about my language problems.
str_replace('types.html' ,'','www.example.com/somthing/types.html');is for if i know the file name (types.html).
You can use this
preg_replace('/([^\/]*$)/', '', 'www.example.com/somthing/types.html');
This will replace all characters after the last /
Output will be www.example.com/somthing/
Try this
$string = 'www.examle.com/somthing/types.html';
echo preg_replace('#\/[^/]*$#', '', $string);
If you are considering the fast and the fastest you may consider this piece of code instead preg_replace
$url = explode("/",$url);
array_pop($url);
$url = implode("/",$url);
Again it is slighly faster so do as you please :).
Proof:
http://sandbox.onlinephpfunctions.com/code/5083e02d4f171502ef1e7c87c8dd9a957ee0d8fb
You can also do this the old fashion way:
$url = 'www.example.com/somthing/types.html';
$divided = explode( '/', $url );
array_pop( $divided );
$new_url = implode( '/', $divided );
Hi i want to know how can i get substring from string after last slash?
In short i want to get the file name from path.
for example i got string like this:
test-e2e4/test-e2e4/test-e2e4/6.png
and i want to get 6.png how can i do that ?
I got dir only and the file name can be all format, it can be also something else then file
Or test-e2e4/test-e2e4/test-e2e4/aaaaa and want to get aaaaa
Regex maybe? Or maybe you know some nice functions which will do it for me ?
In addition to other replies, there's actually a function in PHP to do this: basename. Example:
$string = 'test-e2e4/test-e2e4/test-e2e4/6.png';
$base = basename($string); // $base == '6.png';
$string = 'test-e2e4/test-e2e4/test-e2e4/aaaaa';
$base = basename($string); // $base == 'aaaaa'
$string = '6.png';
$base = basename($string); // $base == '6.png'
Full details here: http://php.net/basename
Do like this..
$yourstring = 'test-e2e4/test-e2e4/test-e2e4/6.png';
$val = array_pop(explode('/',$yourstring)); // 6.png
You can try explode and array_pop functions to work this out:
$str = 'test-e2e4/test-e2e4/test-e2e4/aaaaa.png';
$str = explode('/', $str);
$filename = array_pop($str);
echo $filename; //Output will be aaaaa.png
...Or you can use the following regex:
[^\/]*$
You don't need to use regex for this, you can use substr() to get a portion of the string, and strrpos() to specify which portion:
$full_path = "test-e2e4/test-e2e4/test-e2e4/6.png"
$file = substr( $full_path, strrpos( $full_path, "/" ) + 1 );
substr() returns a portion of the string, strrpos() tells it to start from the position of the last slash in the string, and the +1 excludes the slash from the return value.
string '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg' (length=85)
what i need is just
http://localhost/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
what is the best way doing it ? i mean useing strlen ? substr_replace ? substr ? im a bit confused what is the best way doing this? becouse there is many ways to do this.
edit* there is no newbie tag :|
// get from database red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
$image_path = $this->data['products'][0]['image_small'];
$exploded = end(explode('/', $image_path));
$myurl = DOMAIN;
$myfullurl = $myurl."/storage/".$exploded;
// it works!, but let see the comments maybe there is a better way :)
Here is how you can get the image part:
$str = '/home/adam/Projects/red/storag/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
$exploded = end(explode('/', $str));
echo $exploded;
Result:
22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
Now you can concatenate it with whatever eg:
$new_str = 'http://localhost/storage/' . $exploded;
echo $new_str;
Result:
http://localhost/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
And It is most likely you want to concatenate the image path with your document root which you do like this:
$img_path = $_SERVER['DOCUMENT_ROOT'] . $exploded;
The idea is that you explode the string with explode function by specifying / as delimiter. This gives you array, now you use the end function to get the ending part of the array which is your image actually.
If the path prefix represents your document root path, then you can do this to strip it:
$path = '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
$_SERVER['DOCUMENT_ROOT'] = '/home/adam/Projects/red/';
if (substr($path, 0, strlen($_SERVER['DOCUMENT_ROOT'])) === $_SERVER['DOCUMENT_ROOT']) {
$uriPath = substr($path, strlen(rtrim($_SERVER['DOCUMENT_ROOT'], '/')));
echo $uriPath;
}
I suggest you check if the string contains /home/adam/Projects/red, and if it does, you use substr to get the part after it, and you glue it with http://localost.
$path = '/home/adam/Projects/red/storage/*snip*.jpg';
$basePath = "/home/adam/Projects/red";
if (strpos($path, $path) !== false)
$url = 'http://localhost' . substr($path, strlen($basePath));
This one's pretty much the easiest
str_replace(
"/home/adam/Projects/red",
"http://localhost",
"/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg"
);
$string = '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
str_replace('/home/adam/Projects/red', 'http://localost', $string)