I was using the strrchr PHP function with substr and strrpos to find the file name in a string with the full path like:
/images/onepiece.jpg returns onepiece.jpg
but now I need to a function to find not the last "/" but the one next to the last: /images/anime/onepiece.jpg returning anime/onepiece.jpg or /anime/onepiece.jpg And as strrchr - 1 doesn't work, hehehe :), how can I achieve that?
[SOLVED] Using PHP pathinfo() as @middaparka and @Shakti Singh said, I've change the way I get the image string from the MySQL database. Now it can have subfolders, as was my initial intention.
<?php
/*
* pathinfo() parameters:
* PATHINFO_DIRNAME = 1
* PATHINFO_BASENAME = 2
* PATHINFO_EXTENSION = 4
* PATHINFO_FILENAME = 8
* */
$slash = '/';
$mainpath = 'store/image/';
$thumbspath = 'cache/';
$path = $imgsrow->photo; //gets the string containing the partial path and the name of the file from the database
$dirname = pathinfo($path, 1); //gets the partial directory string
$basename = pathinfo($path, 2); //gets the name of the file with the extension
$extension = pathinfo($path, 4); //gets the extension of the file
$filename = pathinfo($path, 8); //gets the name of the file
$dims = '-100x100.'; //string of size of the file to append to the file name
$image = $mainpath . $path; //mainpath + path is the full string for the original/full size file
$thumbs = $mainpath . $thumbspath . $dirname . $slash . $filename . $dims . $extension; //strin开发者_JAVA技巧g to point to the thumb image generated from the full size file
?>
<img src="<?= $thumbs; ?>" width="100" height="100" alt="<?= $row->description; ?>" />
<br />
<img src="<?= $image; ?>" width="500" height="500" alt="<?= $row->description; ?>" />
To be honest, it would be a lot easier to use the pathinfo or dirname functions to decompose directory paths.
For example:
$filename = pathinfo('/images/onepiece.jpg', PATHINFO_BASENAME);
$directory = dirname('/images/onepiece.jpg');
You may have to use a mix of these to obtain what you're after but they will at least be OS "safe" (i.e.: will handle both Linux/Linux and Windows path styles).
In terms of the specific problem you have, you should be able to use the following cross-platform solution to get what you need:
<?php
$sourcePath = '/images/anime/onepiece.jpg';
$filename = pathinfo($sourcePath, PATHINFO_BASENAME);
$directories = explode(DIRECTORY_SEPARATOR, pathinfo($sourcePath, PATHINFO_DIRNAME));
echo $directories[count($directories) -1] . DIRECTORY_SEPARATOR . $filename;
?>
I would suggest to use explode
to split the path into its segments:
$segments = explode('/', $path);
Then you can use $segments[count($segments)-1]
to get the last path segment.
And for for last two segments you can use array_slice
with implode
to put them back together:
$lastTwoSegments = implode('/', array_slice($segments, -2));
you need to use pathinfo function
pathinfo ($path, PATHINFO_FILENAME );
Dude, just make a temp string to hold the string you want to find. Find the last occurrence, replace it with something, like x, and then find the new last occurrence, which is the second to last occurrence.
精彩评论