PHP strrchr 查找倒数第二个而不是最后一个出现的位置

发布于 2024-10-24 06:11:43 字数 1769 浏览 1 评论 0原文

我使用 strrchr PHP 函数与 substrstrrpos 来查找具有完整路径的字符串中的文件名,例如:

/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

渡你暖光 2024-10-31 06:11:43

老实说,使用 pathinfodirname 函数用于分解目录路径。

例如:

$filename = pathinfo('/images/onepiece.jpg', PATHINFO_BASENAME);
$directory = dirname('/images/onepiece.jpg');

您可能必须混合使用这些来获得您想要的东西,但它们至少是操作系统“安全”的(即:将处理 Linux/Linux 和 Windows 路径样式)。

根据您遇到的具体问题,您应该能够使用以下跨平台解决方案来获得您所需要的:

<?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;
?>

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;
?>
何时共饮酒 2024-10-31 06:11:43

我建议使用 explode 将路径分成几个部分:

$segments = explode('/', $path);

然后您可以使用$segments[count($segments)-1] 获取最后一个路径段。

对于最后两个部分,您可以将 array_sliceimplode 将它们重新组合在一起:

$lastTwoSegments = implode('/', array_slice($segments, -2));

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));
北城挽邺 2024-10-31 06:11:43

您需要使用 pathinfo 函数

pathinfo ($path, PATHINFO_FILENAME );

you need to use pathinfo function

pathinfo ($path, PATHINFO_FILENAME );
梦里的微风 2024-10-31 06:11:43

老兄,只需创建一个临时字符串来保存您要查找的字符串即可。找到最后一个出现的位置,将其替换为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文