我正在使用带有substr和strrpos的strrchr PHP 函数来查找具有完整路径的字符串中的文件名,例如:
/images/onepiece.jpg 返回 onepiece.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; ?>" />