如何获取最后一次出现并显示其余部分?

How do I get the last occurrence and display the rest?

我想知道如何获取字符串中的最后一次出现 并显示之前的内容,例如:

$string = "folder\folder2\folder3\folder4\folder5\folder6\archive.anyextensionhere";

和return:

$result = "folder\folder2\folder3\folder4\folder5\folder6";

我尝试使用 explode (explode("\\", $string)) 但无法继续。如果有人能帮助我,我将不胜感激。

[更新]

抱歉,我忘了说我正在使用数组。

$array = array("hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Episódios [BD 720p]\[Hacchi Fansub]_Shingeki no Kyojin_01 [BD 720p][H264 AAC 8 Bits] [070B7F17].mkv","hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Episódios [BD 720p]\[Hacchi Fansub]_Shingeki no Kyojin_02 [BD 720p][H264 AAC 8 Bits] [F4746E93].mkv","hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Ovas [DVD 480p]\[Hacchi Fansub]_Shingeki no Kyojin_OVA 01 [DVD 480p][H264 AAC 8 Bits] [288FC3D3].mkv","hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Ovas - Kuinaki Sentaku [DVD 480p]\[Hacchi Fansub]_Shingeki no Kyojin - Kuinaki Sentaku_OVA 01 [DVD 480p][H264 AAC 8 Bits] [19570EA3].mkv");

你可以在字符串上爆炸,然后删除最后一个元素,然后像这样再次内爆:

<?php 
$string = "folder\folder2\folder3\folder4\folder5\folder6\archive.anyextensionhere";
$array = explode('\', $string);
$file = array_pop($array);
$new_string = implode("\", $array);

这将首先通过将每个部分放在斜杠之间将字符串拆分为一个数组,然后它将数组的最后一个元素放入 $file 变量中,之后它将删除最后一个元素数组,最后将数组转回字符串

您可以使用 php 的函数 pathinfo,它将 return 数组中您需要的所有内容。

使用 pathinfo 给你 [dirname], [basename], [extension] and [filename] 我相信这是你所请求的所有信息:)

以您的数据为例:

<pre>
    <?php
    $array = array("hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Episódios [BD 720p]\[Hacchi Fansub]_Shingeki no Kyojin_01 [BD 720p][H264 AAC 8 Bits] [070B7F17].mkv","hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Episódios [BD 720p]\[Hacchi Fansub]_Shingeki no Kyojin_02 [BD 720p][H264 AAC 8 Bits] [F4746E93].mkv","hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Ovas [DVD 480p]\[Hacchi Fansub]_Shingeki no Kyojin_OVA 01 [DVD 480p][H264 AAC 8 Bits] [288FC3D3].mkv","hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Ovas - Kuinaki Sentaku [DVD 480p]\[Hacchi Fansub]_Shingeki no Kyojin - Kuinaki Sentaku_OVA 01 [DVD 480p][H264 AAC 8 Bits] [19570EA3].mkv");

        foreach ($array as $arr) {
            print_r (pathinfo ($arr));
        }
    ?>
</pre>

return

Array
(
    [dirname] => hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Episódios [BD 720p]
    [basename] => [Hacchi Fansub]_Shingeki no Kyojin_01 [BD 720p][H264 AAC 8 Bits] [070B7F17].mkv
    [extension] => mkv
    [filename] => [Hacchi Fansub]_Shingeki no Kyojin_01 [BD 720p][H264 AAC 8 Bits] [070B7F17]
)
Array
(
    [dirname] => hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Episódios [BD 720p]
    [basename] => [Hacchi Fansub]_Shingeki no Kyojin_02 [BD 720p][H264 AAC 8 Bits] [F4746E93].mkv
    [extension] => mkv
    [filename] => [Hacchi Fansub]_Shingeki no Kyojin_02 [BD 720p][H264 AAC 8 Bits] [F4746E93]
)
Array
(
    [dirname] => hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Ovas [DVD 480p]
    [basename] => [Hacchi Fansub]_Shingeki no Kyojin_OVA 01 [DVD 480p][H264 AAC 8 Bits] [288FC3D3].mkv
    [extension] => mkv
    [filename] => [Hacchi Fansub]_Shingeki no Kyojin_OVA 01 [DVD 480p][H264 AAC 8 Bits] [288FC3D3]
)
Array
(
    [dirname] => hacc_snk720\[Hacchi Fansub] Shingeki no Kyojin [BD 720p] + Ovas [Completo]\Ovas - Kuinaki Sentaku [DVD 480p]
    [basename] => [Hacchi Fansub]_Shingeki no Kyojin - Kuinaki Sentaku_OVA 01 [DVD 480p][H264 AAC 8 Bits] [19570EA3].mkv
    [extension] => mkv
    [filename] => [Hacchi Fansub]_Shingeki no Kyojin - Kuinaki Sentaku_OVA 01 [DVD 480p][H264 AAC 8 Bits] [19570EA3]
)