简体   繁体   中英

PHP replace multiline string

I have a multiline string like the following (in PHP):

ABCDEF
Colore lato A: NO
Colore lato B: Colore lato B (+3,00)
Colore lato C: NO
Stampa lato A: Stampa lato A (+7,00)
Stampa lato B: NO
Stampa lato C: NO
blablabla
blablabla

basically I would like to make these changes:

  • Remove all lines starting with 'Colore' or 'Stampa' and end with 'NO'
  • Remove the part before ":" lines starting with 'Colore' or 'Stampa'

then the result I would like is the following:

ABCDEF
Colore lato B (+3,00)
Stampa lato A (+7,00)
blablabla
blablabla

For various reasons I can not turn it into array and modify it.

I use regular expressions but I have not found the correct one, you have some idea?

Thank you!

$result = preg_replace('/(' . PHP_EOL . ')?^(Colore|Stampa).*NO|^(Colore|Stampa).*:\s*/m', '', $string);

演示

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM