希维从文件路径字符串中删除前两个目录吗?

问题描述 投票:0回答:3

我有一个字符串“ ./product_image/Bollywood/1476813695.jpg”。

首先我删除。从头开始。

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

Bollywood/1476813695.jpg

我正在尝试使用此方法,但不起作用

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

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

php php-7 filepath substr
3个回答
6
投票

explode()轻松完成:

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

结果:

Bollywood / 1476813695.jpg

如果您想要一些不同的东西,可以将正则表达式与preg_replace()一起使用

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

这将返回相同的内容,并且您可以根据需要将所有内容放在一行中。


2
投票
$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;

1
投票

请遵循以下代码

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

并且输出将看起来是

Bollywood / 1476813695.jpg

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

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

有关主要职位的详细信息,请转到下面的链接

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

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.