我在php中有一个字符串,它只是一个文件路径,即"images/steven/pic.jpg"
php脚本显示所有图像子文件夹中的图片列表,因此最终结果如下:
"images/steven/pic1.jpg"
"images/steven/pic2.jpg"
"images/steven/pic3.jpg"
"images/betty/pic1.jpg"
"images/betty/pic2.jpg"
"images/craig/pic1.jpg"
"images/craig/pic2.jpg"
确实没有必要在每个字符串上都有"images/"
,而且它占用了很多空间,我如何修剪字符串的这一部分,使其输出"steven/pic1.jpg"
等?
字符串在一个数组中,我通过foreach回声,所以我想简单地将trim函数附加到"以保持整洁。
试试这样的东西:
$str = "images/steven/pic1.jpg";
echo substr( $str, strpos( $str, '/') + 1);
这将输出:"steven/pic1.jpg"
如果总是images/
,那么只需执行:
str_replace("images/", "", $yourstring);
怎么样:
preg_replace('#^[^/]*/#', '', 'images/craig/pic2.jpg');