Php 如何从文件路径字符串中删除前两个目录?

Php 如何从文件路径字符串中删除前两个目录?,php,filepath,substr,Php,Filepath,Substr,我有一个字符串。/product_image/宝莱坞/1476813695.jpg 首先,我删除。从一开始 现在我想删除前两个/之间的所有字符。那意味着我想要 Bollywood/1476813695.jpg 我正在尝试,但没有工作 substr(strstr(ltrim('./product_image/Bollywood/1476813695.jpg', '.'),"/product_image/"), 1); 它总是返回产品图片/Bollywood/1476813695.jpg使用ex

我有一个字符串。/product_image/宝莱坞/1476813695.jpg

首先,我删除。从一开始

现在我想删除前两个/之间的所有字符。那意味着我想要

Bollywood/1476813695.jpg
我正在尝试,但没有工作

substr(strstr(ltrim('./product_image/Bollywood/1476813695.jpg', '.'),"/product_image/"), 1);
它总是返回产品图片/Bollywood/1476813695.jpg

使用explode轻松完成:

结果:

宝莱坞/1476813695.jpg

如果您想要一些不同的东西,可以使用带有preg_replace的正则表达式


这将返回相同的内容,如果需要,您可以将其全部放在一行中。

请遵循以下代码

$str = "./product_image/Bollywood/1476813695.jpg";

$str_array = explode('/', $str);

$size = count($str_array);

$new_string = $str_array[$size - 2] . '/' . $str_array[$size - 1];

echo $new_string;
$newstring = "./product_image/Bollywood/1476813695.jpg";
$pos =substr($newstring, strpos($newstring, '/', 2)+1);
var_dump($pos);
而产出将有所增长

宝莱坞/1476813695.jpg

有关strpos功能的详细信息,请转到下面的链接

有关substr职位详情,请访问下面的链接


explode/,$string;然后你有一个数组,可以取你需要的部分,可能str_replace./,/,$string这个答案的可能副本缺少它的教育性解释。Jay,使用不同的模式分隔符和更少的转义,更干净。
$str = "./product_image/Bollywood/1476813695.jpg";

$str_array = explode('/', $str);

$size = count($str_array);

$new_string = $str_array[$size - 2] . '/' . $str_array[$size - 1];

echo $new_string;
$newstring = "./product_image/Bollywood/1476813695.jpg";
$pos =substr($newstring, strpos($newstring, '/', 2)+1);
var_dump($pos);