首页 > 解决方案 > preg_replace - 将所有内容返回到从行尾开始的第二个斜杠

问题描述

我有字符串/fsd/fdstr/rtgd/file/upload/file.png

/upload/file.png我只需要使用 function从这个字符串返回preg_replace

只有我的preg_replace("/.*\/(.*)/", '/$1', $fullPath, -1)回报/file.png

标签: phpregexpreg-replace

解决方案


使用文件系统/路径函数的替代方法(恕我直言,它比正则表达式更具可读性):

$path = '/fsd/fdstr/rtgd/file/upload/file.png';

$folder = dirname($path);
$fileName = basename($path);

$result = '/' . basename($folder) . '/' . $fileName;

演示:https ://3v4l.org/YblcW


推荐阅读