douwen9540 2018-01-10 10:02
浏览 104
已采纳

PHP:preg_replace文件路径

I'm stuggeling with this regular expression:

preg_replace('/\/[\s\S]+?_/', '/', $currentFile)

It changes this: /public_html/ftp/Products/1010_Air+Cooled+Condenser/productsheet_Rotary+Drum+Dryer+BR.pdf

To this: html/Air+Cooled+Condenser/Rotary+Drum+Dryer+BR.pdf

Expected result: /public_html/ftp/Products/Air+Cooled+Condenser/Rotary+Drum+Dryer+BR.pdf

"/productsheet_" could also be "/fish-cb_" or "/appeltaartenbakker_"

"/1010_" is always a number with this length.

  • 写回答

2条回答 默认 最新

  • dprh34164 2018-01-10 10:32
    关注

    I believe you are looking for

    '~(?!^)/[^/]+_~'
    

    See the regex demo. If you need to make it stop before the first _, add the underscore to the [...]: '~(?!^)/[^_/]+_~'.

    Details

    • (?!^) - not at the start of the string
    • / - a / char (no need to escape as I am using ~ as a regex delimiter char)
    • [^/]+ - 1 or more chars other than /
    • _ - a _ char.

    PHP demo:

    $str = '/public_html/ftp/Products/1010_Air+Cooled+Condenser/productsheet_Rotary+Drum+Dryer+BR.pdf';
    $result = preg_replace('~(?!^)/[^/]+_~', '/', $str);
    echo $result; // => /public_html/ftp/Products/Air+Cooled+Condenser/Rotary+Drum+Dryer+BR.pdf
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启