“如何修剪到第一个”/&引用;在php字符串中

“如何修剪到第一个”/&引用;在php字符串中,php,trim,Php,Trim,我在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"

我在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"
$str = "images/steven/pic1.jpg";
echo substr( $str, strpos( $str, '/') + 1);
实际上,每个字符串上都不需要有
“images/”
,而且它占用了很多空间,我如何修剪字符串的这一部分,使其输出
“steven/pic1.jpg”


字符串在一个数组中,我通过foreach回送,因此我只想将trim函数附加到“”以保持其整洁。

尝试以下方法:

"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"
$str = "images/steven/pic1.jpg";
echo substr( $str, strpos( $str, '/') + 1);

这将输出:
“steven/pic1.jpg”

尝试以下操作:

"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"
$str = "images/steven/pic1.jpg";
echo substr( $str, strpos( $str, '/') + 1);

这将输出:
“steven/pic1.jpg”

如果始终是
图像/
,则只需执行以下操作:

str_replace("images/", "", $yourstring);

如果总是
images/
,只需执行以下操作:

str_replace("images/", "", $yourstring);
那么:

preg_replace('#^[^/]*/#', '', 'images/craig/pic2.jpg');
那么:

preg_replace('#^[^/]*/#', '', 'images/craig/pic2.jpg');
注意:str_replace()替换字符串中的所有位置。这是一个不必要的风险。注意:str_replace()替换字符串中的所有位置。这是一个不必要的风险。