2011-03-20 96 views
0

我用的是strrchr PHP函數SUBSTRstrrpos與喜歡全路徑的字符串要查找的文件名:PHP strrchr找到下一個到最後不是最後ocurrence

/images/onepiece.jpg返回 onepiece.jpg

,但現在我需要一個函數來找到不是最後一個「/」,但一個倒數第二: /images/anime/onepiece.jpg返回動漫/ onepiece.jpg/anime/onepiece.jpg 而作爲strrchr - 1不工作,嘻嘻嘻:),我怎麼能做到這一點?

[已解決] 使用PHP pathinfo()作爲@middaparka和@Shakti Singh說,我改變了從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; ?>" /> 
+0

這是一個Opencart的數據庫中,它存儲了圖像名稱與用部分路徑。圖像縮略圖被保存在其他地方,將大小的字符串附加到每個微縮名稱。 – tenshimsm 2011-03-20 20:12:38

回答

1

說實話,這將是一個更容易使用pathinfodirname功能分解目錄路徑。

例如:

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

您可能需要使用這些組合,以獲得您以後在做什麼,但他們至少可以OS「安全」(即:將處理在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; 
?> 
+0

嗨!謝謝你的答案。我不知道這些功能。我會嘗試並讓你知道。 – tenshimsm 2011-03-20 17:17:58

+0

@tenshimsm沒問題。我用一些示例代碼更新了我的答案,這些代碼可以滿足您的要求 - 希望這有助於。 :-) – 2011-03-20 17:19:01

+0

**爲什麼downvote?**嗯......你必須愛那些downvote,但沒有脊椎識別自己或離開理由的人。 :-) – 2011-03-20 17:23:07

0

你需要使用pathinfo功能

pathinfo ($path, PATHINFO_FILENAME); 
+0

-1除了包含語法錯誤(你有一個'''''''''),PATHINFO_FILENAME只會返回文件名組件 - 而不是文件名+擴展名。 – 2011-03-20 17:03:57

+0

嗨!感謝你的回答。我會試試看。 – tenshimsm 2011-03-20 17:18:37

0

老兄,只是做一個臨時串牽繩子的你想找到。找到最後一個匹配項,用x代替它,然後找到新的最後一個匹配項,即第二個匹配項。

1

我會建議使用explode的路徑分成了段:

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

然後你可以使用$segments[count($segments)-1]得到最後路徑段。

併爲最後兩段可以使用array_sliceimplode把他們重新走到一起:

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

+1首先對路徑進行歸一化/規範化是明智之舉。 – Tomalak 2011-03-20 18:10:13

+0

想一想 - 使用DIRECTORY_SEPARATOR常量的一個小調整將使這個跨平臺。 :-) – 2011-03-20 18:13:43

+0

謝謝你的回答。因爲它是URI的一部分,所以我不會遇到操作系統兼容性問題。 :) – tenshimsm 2011-03-20 19:08:42