regex - Delete text form textfile with php -
i try delete texte in text file.
the texte file :
#message :0 * ^(to|cc).*fd.* |/usr/bin/vacation fd #monfiltreperso :0 * ^from.*martial@gironde.com maildir/.repertorymoi #fin #monfiltreperso2 :0 * ^subject:.*monsujet2 maildir/.repertorymoi2 #fin #monfiltreperso3 :0 * ^from.*martial2@gironde.com maildir/.repertorymoi2 #fin
i try delete line between #monfiltre... , #monfiltre...
i have :
$pattern = '~'.'#filtre'.$nom.'\s*\n^#monfiltre~'; $filecontent = preg_replace($pattern, '', $filecontent);
so can delete line :
#monfiltreperso :0 * ^from.*martial@gironde.com maildir/.repertorymoi
but there #fin not deleted.
and have :
#message :0 * ^(to|cc).*fd.* |/usr/bin/vacation fd #fin (need deleted) :0 * ^subject:.*monsujet2 maildir/.repertorymoi2 #fin
i need change rules :
$pattern = '~'.'#filtre'.$nom.'\s*\n^#monfiltre~';
but didn't find how.
if want remove portion between #monfiltre*
until #fin
, can use expression:
echo preg_replace('/^#monfiltreperso$.+?^#fin$[\r\n]*/ms', '', $str);
it uses multiline mode /m
can match against ^
(start of line) , $
(end of line). match across multiple lines, need dot-match-all mode /s
; otherwise match until end of each line.
an optional set of newline characters matched prevent empty lines appearing in end result.
you can make dynamic if want:
$filter = preg_quote('perso3', '/'); echo preg_replace('/^#monfiltre' . $filter . '$.+?^#fin$[\r\n]*/ms', '', $str);
Comments
Post a Comment