我想知道如何在我的字符串中出现最后一次 并显示之前的内容,例如:
subtree
返回:
$string = "folder\folder2\folder3\folder4\folder5\folder6\archive.anyextensionhere";
我尝试使用explode(explode(“\\”,$ string))但我无法继续。如果有人能帮助我,我会感激。
[UPDATE]
抱歉,我忘了提到我正在使用阵列。
$result = "folder\folder2\folder3\folder4\folder5\folder6";
答案 0 :(得分:1)
您可以使用php的函数pathinfo
,它将返回数组中所需的全部内容。
使用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>
返回
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]
)
答案 1 :(得分:0)
你可以在字符串上爆炸,然后删除最后一个元素,然后再次内爆:
<?php
$string = "folder\folder2\folder3\folder4\folder5\folder6\archive.anyextensionhere";
$array = explode('\\', $string);
$file = array_pop($array);
$new_string = implode("\\", $array);
这首先将字符串拆分为数组,然后将数组的最后一个元素放在$file
变量中,之后它将删除数组的最后一个元素最后将数组转回字符串