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


How to remove the first two directories from a file path string?

我有一个字符串"./product_image/Bollywood/1476813695.jpg".

我首先删除

。从第一。

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

Bollywood/1476813695.jpg

我正在尝试这个,但不工作

substr(strstr(ltrim('./product_image/Bollywood/1476813695.jpg', '.'),"/product_image/"), 1);

它总是返回product_image/Bollywood/1476813695.jpg

轻松完成explode():

$orig = './product_image/Bollywood/1476813695.jpg';
$origArray = explode('/', $orig);
$new = $origArray[2] . '/' . $origArray[3];
结果:

1476813695.宝莱坞/jpg

如果你想要一些不同的东西,你可以使用regex与preg_replace()

$pattern = '/'.'/(.*?)'//';
$string = './product_image/Bollywood/1476813695.jpg';
$new = preg_replace($pattern, '', $string);

这将返回相同的内容,如果您愿意,可以将它们全部放在一行中。

$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函数的详细信息,请访问下面的链接

http://php.net/manual/en/function.strpos.php

子字符位置的详细信息请访问下面的链接

http://php.net/manual/en/function.substr.php