我用的是strrchr PHP函數SUBSTR和strrpos與喜歡全路徑的字符串要查找的文件名: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; ?>" />
這是一個Opencart的數據庫中,它存儲了圖像名稱與用部分路徑。圖像縮略圖被保存在其他地方,將大小的字符串附加到每個微縮名稱。 – tenshimsm 2011-03-20 20:12:38