PHP strrchr 查找倒数第二个而不是最后一个出现的位置
我使用 strrchr PHP 函数与 substr 和 strrpos 来查找具有完整路径的字符串中的文件名,例如:
/images/onepiece.jpg 返回 一件作品.jpg
但现在我需要一个函数来查找不是最后一个“/”,而是最后一个旁边的“/”: /images/anime/onepiece.jpg 返回 anime/onepiece.jpg 或 /anime/onepiece.jpg 由于 strrchr - 1 不起作用,呵呵:),我该如何实现呢?
[已解决] 正如 @middaparka 和 @Shakti Singh 所说,使用 PHP pathinfo() ,我改变了从 MySQL 数据库获取图像字符串的方式。现在它可以有子文件夹,这就是我的初衷。
<?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; //string 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; ?>" />
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; //string 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; ?>" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
老实说,使用 pathinfo 或dirname 函数用于分解目录路径。
例如:
您可能必须混合使用这些来获得您想要的东西,但它们至少是操作系统“安全”的(即:将处理 Linux/Linux 和 Windows 路径样式)。
根据您遇到的具体问题,您应该能够使用以下跨平台解决方案来获得您所需要的:
To be honest, it would be a lot easier to use the pathinfo or dirname functions to decompose directory paths.
For example:
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:
我建议使用
explode
将路径分成几个部分:然后您可以使用
$segments[count($segments)-1]
获取最后一个路径段。对于最后两个部分,您可以将
array_slice
与implode
将它们重新组合在一起:I would suggest to use
explode
to split the path into its segments: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
withimplode
to put them back together:您需要使用 pathinfo 函数
you need to use pathinfo function
老兄,只需创建一个临时字符串来保存您要查找的字符串即可。找到最后一个出现的位置,将其替换为 x 等内容,然后找到新的最后一个出现位置,即倒数第二个出现位置。
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.