![](/img/trans.png)
[英]Trim only the first and last occurrence of a character in a string (PHP)
[英]How to trim to first “/” in php string
我在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');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.