php - preg_replace - 将所有内容返回到从行尾开始的第二个斜杠
问题描述
我有字符串/fsd/fdstr/rtgd/file/upload/file.png
。
/upload/file.png
我只需要使用 function从这个字符串返回preg_replace
。
只有我的preg_replace("/.*\/(.*)/", '/$1', $fullPath, -1)
回报/file.png
。
解决方案
使用文件系统/路径函数的替代方法(恕我直言,它比正则表达式更具可读性):
$path = '/fsd/fdstr/rtgd/file/upload/file.png';
$folder = dirname($path);
$fileName = basename($path);
$result = '/' . basename($folder) . '/' . $fileName;
推荐阅读
- javascript - 如何使用 axios 进行编码的 POST 请求?
- angular - 与使用管道读取值相比,角度模板中的硬编码对象属性在浏览器中的性能是否更好
- php - 致命错误:未捕获错误:调用未定义函数 mysql_close()
- asynchronous - Chai 测试 .then 内部抛出的错误
- angular - 滚动到顶部时进度条动画的问题
- python - 如何将 numpy.ndarray 作为元数据包含在 boto3 中?
- sql - 如何生成 2 级循环
- arrays - 检查二维数组是升序还是降序排序
- c++ - 解析文件以将变量分配给结构。C++
- hibernate - 如何从java中查询/调用hibernate的@JoinTable注解