在特定字符串之前删除而不是字符

remove before specific string not character

string格式如下

$img = "/images/posts/main.jpg";
$img1 = "/images/posts/john.jpg";

我需要删除 /images/posts/ 并回显其余内容。

我试过 strstr,但我发现它只处理字符

echo strstr($img, '/images/posts/')
//output => /images/posts/main.jpg

如果我只使用单个字符 echo strstr($img, '/') 那么输出是 images/posts/.

所以我使用 substrstrstr 来获得预期的结果。

echo substr(strstr($img, '/'), 14);
//output => main.jpg

在我的例子中,我确信它会一直运行并得到相同的结果,因为 images/posts/ 部分保持不变,不会改变。

但这种计数和修整的方法真的好还是快?还有其他快速削减 /images/posts/ 的方法吗?

或者替换?更快吗??

echo str_replace('/images/posts/','',$img);

你可以这样做..

语法-

 str_replace(find, replace, string, count) 

例如-

str_replace('/images/posts/', '', '/images/posts/main.jpg');

它将打印 main.jpg