i have a variable that shows me the path to a directory like below:
$dir = uploads/sha256/folder1/subfolder1/subsubfolder1
How can i "cut off" the first 2 directories from $dir so that it becomes:
$dir = folder1/subfolder1/subsubfolder1
sample code:
$dir = "uploads/sha256/folder1/subfolder1/subsubfolder1";
$pieces = explode("/", $dir);
echo $pieces[2]; // piece2
This gives me only folder1
And i need the complete path after the sha256
so what i actually try to achieve is something like this:
echo $pieces[>2];
You can capture () everything after the first two directories and replace with that:
$dir = preg_replace('#[^/]+/[^/]+/(.*)#', '$1', $dir);
Or you can explode it, slice all elements after the first two and implode it again:
$dir = implode('/', array_slice(explode('/', $dir), 2));
Related
I want Get Matched Files in php I have try Many More Time
for example ...
my directory files
1.254450_abcd.mp3
2.101215_apple.mp4
3.102545_efgf.php
i find only number like this 254450
$mypath = "/files/" ;
$find = "254450" ;
//i want get matched full name
echo "$filename" ;// get 254450_abcd.mp3
else
"file not found " ;
You can use scandir and preg_grep.
$mypath = "/files/" ;
$find = "254450" ;
$files = scandir($mypath);
$matches = preg_grep("/" . $find . "/", $files);
$matches is now an array with files matching $find
Here is a semi working example. I replaced scandir with your files in an array, just like scandir returns them.
https://3v4l.org/QullZ
$path = parse_url($post->guid, PHP_URL_PATH);
echo "<pre>";
print_r($path);
echo "<br>";
here i get
/wp-content/uploads/2014/01/kl-2-256.png
/wp-content/uploads/2014/04/bg-eBook.pdf
here i want to remove /wp-conent/uploads from these paths and extract only year month and image name
i tried with
$segments = explode('/', rtrim($path, '/'));
but not working properly every time
is there any proper and best solution?
Would something like this work for you?
$path = parse_url($post->guid, PHP_URL_PATH);
$path = str_replace("wp-content/uploads/", "", $path);
echo $path;
Use the list() construct to map the three data you need. The code is exploding the path by / and then looks from behind and passes those values to your mapped variables of list.
$path = '/wp-content/uploads/2014/01/kl-2-256.png';
list($year,$month,$image)=array_slice(explode('/',$path),-3,3);
You can then print $year,$month and $image separately.
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
I have a path like this
apples/oranges/bananas
I need to get the middle item in the path, in this case oranges.
What is the best way to do it? I can do it myself using strpos and substr but I imagine there is a better way...
$path = explode("/", "apples/oranges/bananas");
echo $path[1];
You could explode the string (assuming it is) and then get the correct index from the array. Like so:
$string = "apples/oranges/bananas";
$array = explode('/', $string);
echo $array[1]; //outputs oranges
Just to show off array dereferencing in PHP > 5.4:
echo explode('/', 'apple/oranges/bananas')[1];
If
$path = 'apples/oranges/bananas';
you could do:
$dir = basename(dirname($path));
if you want to start from the end of the string, and should work on Windows, or
$dir = preg_match('|/([^/]*)|', $path, $m) ? $m[1] : false;
if you want to start at the beginning of the string, and will not work on Windows.
Is it always 3 words separated by 2 slashes?
if yes, you can try:
$mypath = explode('/', 'apple/oranges/bananas');
echo $mypath[1]; //gives oranges
I have a variable like $path = "dir1/dir2/dir1/dir4/"; etc.. etc..
I want to remove the first member dir1/ and want result like dir2/dir1/dir4/.
I think it is possible by making the variable an array by explode('/', $path). How can I remove the first member vrom array and reconstruct that array into a text variable??
How can I achieve this in PHP?
According to your updated question
Only explode into two parts, take the second one. In case the second one does not exists, give it NULL:
list(, $result) = explode("/", $path, 2) + array( 1 => NULL);
OR
$array = explode("/", $path);
unset($array[0]);
echo $text = implode("/", $array);
preg_replace('~^[^/]+/~', '', $path);
or if you don't want regexp:
substr($path, strpos($path, '/') + 1);
$result = explode("/", $path); // Pull it apart
array_shift($result); // Pop the first index off array
$result = implode("/", $result); // Put it together again
You can do like that
$result= explode("/", $path);.
You will get result as an array.