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.
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.$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
Depending on how well formed those file paths are you could try:
preg_replace('/[0-9]{4}_(.+\/).+_/','\1',$currentFile)
Which I tested in php 7.0.3 and in vim with:
/public_html/ftp/Products/1010_Air+Cooled+Condenser/productsheet_Rotary+Drum+Dryer+BR.pdf
/public_html/ftp/Products/1020_Air+Cooled+Condenser/fish-cb_Rotary+Drum+Dryer+BR.pdf
/public_html/ftp/Products/1030_Air+Cooled+Condenser/appeltaartenbakker_Rotary+Drum+Dryer+BR.pdf
Using this command and very magic mode \v
:
:%s/\v[0-9]{4}_(.+\/).+_/\1/g
/public_html/ftp/Products/Air+Cooled+Condenser/Rotary+Drum+Dryer+BR.pdf
/public_html/ftp/Products/Air+Cooled+Condenser/Rotary+Drum+Dryer+BR.pdf
/public_html/ftp/Products/Air+Cooled+Condenser/Rotary+Drum+Dryer+BR.pdf